How to get the number of Firestore documents in flutter
Solution 1
Firebase doesn't officially give any function to retrieve the number of documents in a collection, instead you can get all the documents of the collection and get the length of it..
There are two ways:
1)
final int documents = await Firestore.instance.collection('products').snapshots().length;
This returns a value of int. But, if you don't use await, it returns a Future.
2)
final QuerySnapshot qSnap = await Firestore.instance.collection('products').getDocuments();
final int documents = qSnap.documents.length;
This returns a value of int.
However, these both methods gets all the documents in the collection and counts it.
Thank you
Solution 2
It Should Be - Firestore.instance.collection('products').snapshots().length.toString();
Solution 3
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}");
});
Solution 4
Future getCount({String id}) async => FirebaseFirestore.instance
.collection(collection) //your collectionref
.where('deleted', isEqualTo: false)
.get()
.then((value) {
var count = 0;
count = value.docs.length;
return count;
});
this is in dart language...
Solution 5
Since you are waiting on a future, this must be place within an async function
QuerySnapshot productCollection = await
Firestore.instance.collection('products').get();
int productCount = productCollection.size();
Amount of documents in the collection
Related videos on Youtube
aaww223
Updated on June 04, 2022Comments
-
aaww223 almost 2 years
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.
-
Doug Stevenson over 5 yearsNote that this will cause the client to download all of the documents in the collection just to get the count. For large collections, this could be extremely inefficient and costly.
-
Olantobi over 5 years@DougStevenson, What is the best way to get the count?
-
Doug Stevenson over 5 years@Olantobi The recommended solution is in the documentation. firebase.google.com/docs/firestore/solutions/counters
-
Fayaz over 4 years@DougStevenson is the counter method is similar to incrementing count, whenever a child is added?
-
Robert Williams almost 4 yearswhat if i have thousands of documents in each collection?
-
Robert Williams almost 4 yearshow can u do this with remote config?
-
Bisclavret almost 4 yearsThis returns a type of Future<int>, so how do you do anything at all with this? It's not even valuable as a string, in the example you have provided, as it just shows errors, and the value of the string is "Instance of Future<int>"
-
user3698694 over 3 yearsI think that's not going to work... as it would return the future int... where as you would need this in streambuilder or future builder. Please share the complete snippet
-
Shaybc over 3 yearsthis solution can easily go out of sync (due to failed updates or malformed transaction isolation in the app side...), it is always a good practice to get a real data like a select count with a where clause or a Firestore.instance.collection('products').snapshots().length; (snapshots() is a stream and wont cause downloading all the documents in the database if you don't want to)
-
Joe Ferndz over 3 yearsPlease don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes