【问题标题】:Count document where value matched in firestore Flutter在 Firestore Flutter 中计算值匹配的文档
【发布时间】:2021-04-27 10:17:40
【问题描述】:

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

流生成器:

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

    }

【问题讨论】:

  • Firebase 实时数据库和 Cloud Firestore 是两个独立的数据库。请仅使用相关标签标记您的问题,不要同时使用两者。

标签: firebase flutter google-cloud-firestore


【解决方案1】:

您必须直接在快照的 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;

Future 的另一种使用方式:

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

【讨论】:

    【解决方案2】:

    所以我知道这个问题的解决方案,所以我发布了上述问题的示例

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

    【讨论】:

      猜你喜欢
      • 2021-05-08
      • 2011-09-26
      • 2021-04-26
      • 1970-01-01
      • 2019-03-19
      • 2022-11-22
      • 2018-07-17
      • 2020-11-06
      • 2018-09-03
      相关资源
      最近更新 更多