Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asynchronous 如何在执行下一条指令之前完成异步未来任务_Asynchronous_Dart_Flutter_Dart Async - Fatal编程技术网

Asynchronous 如何在执行下一条指令之前完成异步未来任务

Asynchronous 如何在执行下一条指令之前完成异步未来任务,asynchronous,dart,flutter,dart-async,Asynchronous,Dart,Flutter,Dart Async,我对我的数据库进行函数调用,在获取数据后更新本地对象,这需要一些时间 由于异步任务,程序将移动到下一行代码。不幸的是,我需要一个本地对象,该对象通过下一行代码的异步调用进行更新 在执行下一段代码之前,如何等待异步任务完成?多谢各位 编辑:添加代码以解释 updateUser() { return FutureBuilder( future: updateUserData(), builder: (BuildContext context, AsyncSnapsho

我对我的数据库进行函数调用,在获取数据后更新本地对象,这需要一些时间

由于异步任务,程序将移动到下一行代码。不幸的是,我需要一个本地对象,该对象通过下一行代码的异步调用进行更新

在执行下一段代码之前,如何等待异步任务完成?多谢各位

编辑:添加代码以解释

updateUser() {
return FutureBuilder(
        future: updateUserData(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState != ConnectionState.done) {
            return Text("hello not");
          } else {
            return Text('Hello!');
          }
        },

);}

  @override
  Widget build(BuildContext context) {
    switch (_authStatus) {
      case AuthStatus.notSignedIn:
        return new LoginPage(
          auth: auth,
          CurrentUser: CurrentUser,
          onSignedIn: _signedIn,
        );
      case AuthStatus.signedIn:
        {
          updateUser(); //THIS TAKES A COUPLE SECONDS TO FINISH BUT I NEED TO SEND IT TO THE NEXT PAGE

            return new HomePage(
              auth: auth,
              CurrentUser: CurrentUser,
              onSignedOut: _signedOut,
            );
          }
        }
    }
  }

您可以在
async
函数中使用
wait
关键字

例如:


在这里,您的代码块在someFutureFunction返回某些内容之前不会运行。

这可能会有所帮助,下面的示例代码有两个函数, 下面的函数用于加载资产并返回JSON内容

Future<String> _loadCountriesAsset() async {
  return await rootBundle.loadString('assets/country_codes.json');
}

希望这有帮助

您还可以与自定义异步函数一起使用,如以下示例所示:

(() async {
   await restApis.getSearchedProducts(widget.sub_cats_id,widget.keyword).then((val) => setState(()
   {
      setState(() {
         data = val["data"];
      });
   }));
})();

如何等待?使用
await
了解更多有关
await
干净简单的答案
Future<List<CountryCode>> loadCountryCodes() async {
  String jsonString = await _loadCountriesAsset();
  final jsonResponse = json.decode(jsonString);
  // print(jsonResponse);

  CountriesCodeList countriesCodeList =
      new CountriesCodeList.fromJson(jsonResponse);
  // print("length " + countriesCodeList.codes.length.toString());
  return countriesCodeList.codes;
}
Future getCountryCodes() async {
var countryCodesList = await loadCountryCodes();

print("**** length " + countryCodesList.length.toString());
// Perform the operations, or update to the UI or server. It should be same for API call as well. 
}
(() async {
   await restApis.getSearchedProducts(widget.sub_cats_id,widget.keyword).then((val) => setState(()
   {
      setState(() {
         data = val["data"];
      });
   }));
})();