首页 > 解决方案 > 在 Firestore Flutter 中计算值匹配的文档

问题描述

我得到了 20 条评论的价值,但我只在我的帖子上输入了两条评论,我没有找到这个解决方案。在上面我使用流生成器的错误中,我在哪里对我的文本进行查询。

流生成器:

 StreamBuilder(
   stream: FirebaseFirestore.instance.collection("post").snapshots(),
     builder: (BuildContext context,AsyncSnapshot<QuerySnapshot>snapshot){

       return ListView.builder(
               shrinkWrap: true,
               scrollDirection: Axis.vertical,
                itemCount: snapshot.data.docs.length,
                 physics: NeverScrollableScrollPhysics(),
                 itemBuilder: (_,index) {
               return 
     Text('${FirebaseFirestore.instance.collection("comments").where("postid",isEqualTo: 
     snapshot.data.docs[index].id).get().toString().length}')

    }

标签: firebasefluttergoogle-cloud-firestore

解决方案


您必须.length直接使用快照的 docs 属性,如下所示:

final QuerySnapshot snapshot = await Firestore.instance.collection('products').where("postid",isEqualTo: snapshot.data.docs[index].id).get();
final int documents = snapshot.docs.length;

使用 Future 的另一种方式:

FirebaseFirestore.instance
        .collection("products")
        .where("postid", isEqualTo: snapshot.data.docs[index].id)
        .get()
        .then((snapshot) => {
                print(snapshot.docs.length);
            });

推荐阅读