How to get the number of Firestore documents in flutter

这一生的挚爱 提交于 2020-01-21 17:44:45

问题


I am building a flutter app and using Cloud Firestore. I want to get the number of all documents in the database.

I tried

Firestore.instance.collection('products').toString().length

but it didn't work.


回答1:


First, you have to get all the documents from that collection, then you can get the length of all the documents by document List. The below could should get the job done.

Firestore.instance.collection('products').getDocuments.then((myDocuments){
 print("${myDocuments.documents.length}");
});



回答2:


It Should Be - Firestore.instance.collection('products').snapshots().length.toString();




回答3:


Above suggestions will cause the client to download all of the documents in the collection to get the count. As a workaround, if your write operations are not frequently happening, you can put the length of the documents to firebase remote config and change it whenever you add/delete documents from the Firestore collection. Then you can fetch the length from firebase remote configs when you need it.



来源:https://stackoverflow.com/questions/53352632/how-to-get-the-number-of-firestore-documents-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!