Count document where value matched in firestore Flutter

Viewed 553

i m getiing 20 comments value but i only enter only two comments on my post i m not finiding this solution. in the above i m using stream builder error is there where i m putting query on my text.

stream builder:

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

    }
2 Answers

You must use the .length on the snapshot's docs property directly as shown below:

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:

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