如何正确地等到飞镖的未来完成 [英] How to properly wait until future is complete in dart

查看:58
本文介绍了如何正确地等到飞镖的未来完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Flutter制作的应用程序中存在一个小错误,即当用户登录时,它会从我的数据库中获取用户信息,但速度不够快,并在应用程序的前端引起视觉错误.该应用程序具有使用用户信息(名称,位置和图像)的布局,并且加载速度不够快.我想知道是否有一种方法可以等待我的未来完成,一旦完成,它就可以毫无问题地将用户导航到前端.

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 visual error on my front end of the app. The app has layouts that use the user information (name, location, and image) and it is not being loaded quick enough. I was wondering if there is a way to wait for my future to complete and once it is done, it can navigate the user to the front end with no problem.

推荐答案

您应该在initState()函数中从数据库中获取日期,然后必须修改窗口小部件生成器以使用FutureBuilder,这是一个示例:

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();
      }
    }
  )
}

请注意,您可以将AsyncSnapshot<SharedPreferences>替换为Future函数返回的类型.

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

这篇关于如何正确地等到飞镖的未来完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆