How to properly wait until future is complete in dart

前端 未结 1 1989
广开言路
广开言路 2020-12-12 04:46

There is a slight bug in my app made with Flutter, that when the user has signed in, it fetches the user information from my database but not fast enough and causes a visua

相关标签:
1条回答
  • 2020-12-12 05:10

    You Should fetch your date from the database in the initState() function, then you have to modify your widget builder to use FutureBuilder, here's an example:

    Widget build(BuildContext context) {
    return FutureBuilder(
        future: getProfile(),
        builder: (BuildContext context, AsyncSnapshot<SharedPreferences> snapshot) {
          if(snapshot.connectionState == ConnectionState.done){
            return new MaterialApp();
          }
        }
      )
    }
    

    note that you can replace AsyncSnapshot<SharedPreferences> with the type your Future function returns.

    0 讨论(0)
提交回复
热议问题