【问题标题】:Wait a function to be done in ListView等待一个函数在 ListView 中完成
【发布时间】:2021-07-03 02:08:41
【问题描述】:

由于 Firestore 请求,我需要等待我的函数在 ListView 中完成。我尝试使用 Future.wait() 但它不起作用。

FutureBuilder<QuerySnapshot>(
                    future: FirebaseFirestore.instance
                        .collection('Statements')
                        .where('userID',isEqualTo: context.watch<User>().uid)
                        .get(),
                    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
                      if (snapshot.hasError) {return Text("Erreur");}
                      if (snapshot.connectionState == ConnectionState.waiting) {
                        return Text("Loading");}
                      return Expanded(
                        child:ListView.builder(
                          itemCount: snapshot.data.docs.length,
                            itemBuilder: (context, index) {
                              DocumentSnapshot document = snapshot.data.docs[index];
                              Future.wait([getStatementData(document)]);
                              return StatementCard(statement:selectedStatement,
                                           accommodation : relatedAccommodation,
                                           owner : relatedOwner);
                            },
                        )

这是调用的函数:

Future getStatementData(document)  async {

    selectedStatement = Statement.fromFirebase(document);

    document.data()["accommodation"].get().then((value) {
      relatedAccommodation = Accommodation.fromFirebase(value);});

     await FirebaseFirestore.instance
         .collection('Owners')
         .doc(document["ownerList"][0])
         .get().then((value) {
           print(value.data());
           relatedOwner = Owner.fromFirebase(value);
     });

  }

我应该使用另一个未来的构建器吗?

【问题讨论】:

    标签: flutter listview google-cloud-firestore flutter-futurebuilder


    【解决方案1】:

    你应该这样做,在里面返回另一个 Future Builder:

       ListView.builder(
              itemCount: snapshot.data.docs.length,
              itemBuilder: (context, index) {
             DocumentSnapshot document = snapshot.data.docs[index];
            return  FutureBuilder(
          future: getStatementData(document) ,
          builder: (context, snapshot) {
            return snapshot.connectionState == ConnectionState.done
                   ? StatementCard(statement:selectedStatement,
                    accommodation : relatedAccommodation,
                     owner : relatedOwner);
                   : Container(); 
        );
          },
             )
    
    

    【讨论】:

      【解决方案2】:

      我认为您只是错过了一个简单的事情,FutureBuilder 也有一个您可以访问的 connectionstate.done 状态。这会等到您未来的功能完成。

      return FutureBuilder(
                  future: yourFunction,
                  builder: (context, snapshot) {
                    switch (snapshot.connectionState) {
                      case ConnectionState.none:
                        return Center(child: Text('No status',);
                        break;
                      case ConnectionState.waiting:
                        return Center(child: CircularProgressIndicator());
                        break;
                      case ConnectionState.done:
                      return Text('Im done!') // Your listviewbuilder comes here
                        break;
                      default:
                    }
                  }, 
                ),
      

      文档:https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html 带有 ListViewBuilder 的 FutureBuilder 示例:https://medium.com/nonstopio/flutter-future-builder-with-list-view-builder-d7212314e8c9

      [...]connectionState.done = [future] 不为空,并且已完成。如果未来成功完成,[AsyncSnapshot.data] 将设置为未来完成的值。如果它以错误完成,[AsyncSnapshot.hasError] 将为真,[AsyncSnapshot.error] 将设置为错误对象。[...]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-31
        • 2019-06-16
        • 2020-06-10
        • 1970-01-01
        相关资源
        最近更新 更多