简体   繁体   English

计数在 Firestore Flutter 中值匹配的文档

[英]Count document where value matched in firestore Flutter

im getiing 20 comments value but i only enter only two comments on my post im not finiding this solution.我得到了 20 条评论的价值,但我只在我的帖子上输入了两条评论,我没有找到这个解决方案。 in the above im using stream builder error is there where im putting query on my text.在上面我使用 stream 构建器错误时,我对我的文本进行了查询。

stream builder: stream 建造者:

 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}')

    }

You must use the .length on the snapshot's docs property directly as shown below:您必须直接在快照的 docs 属性上使用.length ,如下所示:

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

Another way using Future:使用 Future 的另一种方式:

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

so i know the solution of this so i post the example of above problem所以我知道这个问题的解决方案,所以我发布了上述问题的示例

StreamBuilder(stream:FirebaseFirestore.instance.collection("comment").where("postid",isEqualTo: snapshot.data.docs[index].id).snapshots(),
                                                        builder: (context,projsnap){
                                                        final int documents = projsnap.data.docs.length;
                                                        return Text('${documents}');
                                                    }),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM