Flutter 即使查询未完成,Future Builder也会完成数据获取(Flatter Firestore)

Flutter 即使查询未完成,Future Builder也会完成数据获取(Flatter Firestore),flutter,google-cloud-firestore,Flutter,Google Cloud Firestore,我创建了一个flatter应用程序,它可以从多个来源、firestore数据库和一些HTTP API调用获取数据。我知道您需要时间将这些数据加载到应用程序中,因此我在FutureBuilder()上构建了我的小部件。但是,有时快照在加载所有数据之前已经返回了数据。有人能帮我延长加载程序动画的时间,这样我的UI就不会出错了。非常感谢 以下是我的主要小部件: Widget build(BuildContext context) { print('this is homelist leng

我创建了一个flatter应用程序,它可以从多个来源、firestore数据库和一些HTTP API调用获取数据。我知道您需要时间将这些数据加载到应用程序中,因此我在
FutureBuilder()
上构建了我的小部件。但是,有时快照在加载所有数据之前已经返回了数据。有人能帮我延长加载程序动画的时间,这样我的UI就不会出错了。非常感谢

以下是我的主要小部件:

  Widget build(BuildContext context) {
    print('this is homelist length: $homeList');
    return Scaffold(
      backgroundColor: AppTheme.white,
      body: FutureBuilder(
        future: getData(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(
                strokeWidth: 3,
              ),
            );
          } else {
            return Padding(
              padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  appBar(),
                  Expanded(
                    child: FutureBuilder(
                      future: getData2(),
                      builder: (context, snapshot) {
                        if (!snapshot.hasData) {
                          return Center(
                            child: CircularProgressIndicator(
                              strokeWidth: 3,
                            ),
                          );
                        } else {
                          return homeList.isEmpty == false
                              ? GridView(
                                  padding: EdgeInsets.only(
                                      top: 0, left: 12, right: 12),
                                  physics: BouncingScrollPhysics(),
                                  scrollDirection: Axis.vertical,
                                  children: List.generate(
                                    homeList.length,
                                    (index) {
                                      var count = homeList.length;
                                      var animation =
                                          Tween(begin: 0.0, end: 1.0).animate(
                                        CurvedAnimation(
                                          parent: animationController,
                                          curve: Interval(
                                              (1 / count) * index, 1.0,
                                              curve: Curves.fastOutSlowIn),
                                        ),
                                      );
                                      animationController.forward();
                                      return HomeListView(
                                        animation: animation,
                                        animationController:
                                            animationController,
                                        listData: homeList.elementAt(index),
                                        callBack: () {
                                          Navigator.push(
                                            context,
                                            MaterialPageRoute(
                                              builder: (context) => homeList
                                                  .elementAt(index)
                                                  .navigateScreen,
                                            ),
                                          );
                                        },
                                      );
                                    },
                                  ),
                                  gridDelegate:
                                      SliverGridDelegateWithFixedCrossAxisCount(
                                    crossAxisCount: multiple ? 2 : 1,
                                    mainAxisSpacing: 12.0,
                                    crossAxisSpacing: 12.0,
                                    childAspectRatio: 1.5,
                                  ),
                                )
                              : Container(
                                  margin: EdgeInsets.all(12),
                                  child: Column(
                                    children: <Widget>[
                                      SizedBox(
                                        height: 30,
                                      ),
                                      Container(
                                        decoration: BoxDecoration(
                                          color: AppTheme.white,
                                          borderRadius: BorderRadius.only(
                                              topLeft: Radius.circular(8.0),
                                              bottomLeft: Radius.circular(8.0),
                                              bottomRight: Radius.circular(8.0),
                                              topRight: Radius.circular(8.0)),
                                          boxShadow: <BoxShadow>[
                                            BoxShadow(
                                                color: AppTheme.grey
                                                    .withOpacity(0.8),
                                                offset: Offset(1.1, 1.1),
                                                blurRadius: 10.0),
                                          ],
                                        ),
                                        child: Column(
                                          children: <Widget>[
                                            Text(
                                                'Welcome to DiabeatIS, your patients will be displayed here!',
                                                style: AppTheme.headline,
                                                textAlign: TextAlign.justify),
                                            Divider(
                                              color: Colors.black,
                                            ),
                                            SizedBox(
                                              height: 15,
                                            ),
                                            Text(
                                              'Simply ask your patient for their code, and click the button below, you will be taken to a screen where you can add your patients, and view their data afterwards!',
                                              style: AppTheme.subtitle,
                                              textAlign: TextAlign.justify,
                                            ),
                                            RaisedButton(
                                              child: Text('Add Patients'),
                                              onPressed: () {
                                                Navigator.push(
                                                    context,
                                                    MaterialPageRoute(
                                                        builder: (context) =>
                                                            AddPatient()));
                                              },
                                            )
                                          ],
                                        ),
                                      ),
                                    ],
                                  ),
                                );
                        }
                      },
                    ),
                  ),
                ],
              ),
            );
          }
        },
      ),
    );
  }

小部件构建(构建上下文){
打印('这是homelist长度:$homelist');
返回脚手架(
背景颜色:AppTheme.white,
正文:未来建设者(
future:getData(),
生成器:(上下文,快照){
如果(!snapshot.hasData){
返回中心(
子对象:循环压缩机指示器(
冲程宽度:3,
),
);
}否则{
返回填充(
padding:EdgeInsets.only(top:MediaQuery.of(context.padding.top)),
子:列(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
appBar(),
扩大(
孩子:未来建设者(
future:getData2(),
生成器:(上下文,快照){
如果(!snapshot.hasData){
返回中心(
子对象:循环压缩机指示器(
冲程宽度:3,
),
);
}否则{
return homeList.isEmpty==false
?网格视图(
填充:仅限边缘设置(
顶部:0,左侧:12,右侧:12),
物理:弹跳CrollPhysics(),
滚动方向:轴垂直,
子项:List.generate(
homeList.length,
(索引){
var count=homeList.length;
var动画=
吐温(开始:0.0,结束:1.0)。设置动画(
曲线化(
父对象:animationController,
曲线:区间(
(1/计数)*指数,1.0,
曲线:曲线。快速输出),
),
);
animationController.forward();
返回HomeListView(
动画:动画,
animationController:
动画控制器,
listData:homeList.elementAt(索引),
回调:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>homeList
.elementAt(索引)
.导航屏幕,
),
);
},
);
},
),
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
交叉轴计数:多个?2:1,
主轴间距:12.0,
横轴间距:12.0,
儿童方面:1.5,
),
)
:容器(
保证金:所有(12),
子:列(
儿童:[
大小盒子(
身高:30,
),
容器(
装饰:盒子装饰(
颜色:AppTheme.white,
borderRadius:仅限borderRadius(
左上:半径。圆形(8.0),
左下角:半径。圆形(8.0),
右下角:半径。圆形(8.0),
右上角:半径。圆形(8.0)),
boxShadow:[
箱形阴影(
颜色:AppTheme.grey
.不透明度(0.8),
偏移量:偏移量(1.1,1.1),
半径:10.0),
],
),
子:列(
儿童:[
  Future<bool> getData2() async {
    await Future.delayed(Duration(milliseconds: 200));
    return true;
  }

  Future<bool> getData() async {
    _getCurrentUser();
    await Future.delayed(const Duration(milliseconds: 1000), () async {
      _getPatients(); // a function that involves two queries 
    });


    return true;
  }

  Future<bool> _getPatients() async {
    homeList.clear();
    if (didLoadpatients == 0) {
      print('this is didloadpatients at start of func $didLoadpatients');
      var document = await db
          .collection('users')
          .document(mUser.uid)
          .collection('patients');

      document.getDocuments().then((QuerySnapshot query) async {
        query.documents.forEach((f) {
          uids.add(f.data['uID']);
        });
        didLoadpatients++;
        print('this is didloadpatients at end of func $didLoadpatients');
        for (var i = 0; i < uids.length; i++) {
          var userDocuments = await db.collection('users').document(uids[i]);
          userDocuments.get().then((DocumentSnapshot doc) {
            homeList.add(HomeList(
                imagePath: 'assets/fitness_app/fitness_app.png',
                navigateScreen: DVFitnessAppHomeScreen(
                  uid: doc.data['userID'],
                ),
                patientInfo: new PatientInfo.fromFbase(doc.data)));
          });
          print(homeList);
        }
        print(homeList);

        var document = await db
            .collection('users')
            .document(mUser.uid)
            .collection('notifications')
            .where('hasbeenViewed', isEqualTo: false)
            .getDocuments();

        final List<DocumentSnapshot> notifDoc = document.documents;

        notifications = notifDoc.length;

        await Future.delayed(Duration(milliseconds: 800));
        return true;
      });
    } else
      print('I am leaving the get patient function');
    await Future.delayed(Duration(milliseconds: 800));
    return true;
  }

 Future<bool> getData() async {
     await _getCurrentUser();
     await _getPatients();

    return true;
  }
Future.wait([future1, future2])