Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 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 Reply

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...