Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

android - How to count the number of documents firestore on flutter?

I want to calculate how many documents are in a collection, not the length of the document. I have tried it with some code but what appears is the length of the character from my document name.

enter image description here

this my code :

StreamSubscription<DocumentSnapshot> userpost;
    final DocumentReference documentReference =
        Firestore.instance.document("product/$documentPost");
    userpost = documentReference.snapshots().listen((datasnapshot) {
      if (datasnapshot.exists) {
        for (int i = 0; i < datasnapshot.data.length; i++){
           print(datasnapshot.data.length);
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An Example Function to fetch Documents Count.

void countDocuments() async {
    QuerySnapshot _myDoc = await Firestore.instance.collection('product').getDocuments();
    List<DocumentSnapshot> _myDocCount = _myDoc.documents;
    print(_myDocCount.length);  // Count of Documents in Collection
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...