I’m trying to fetch some data from firebase firestore.
Below is the code that i used to fecth data
Future<List<Candidate>> findCandidateByName(String name) async {
final docs = await FirebaseFirestore.instance
.collection('candidates')
.where('name', isEqualTo: name)
.get()
.then((snapshot) => snapshot.docs);
return docs.map((doc) => Candidate.fromDoc(doc)).toList();
}
However, whenever i call this function app crashes while querying.
Below is the debug console message before the app crashing
E/AndroidRuntime(25623): java.lang.OutOfMemoryError: Firestore (24.1.2) ran out of memory. Check your queries to make sure they are not loading an excessive amount of data.
I’m thinkg that the problem is the size of the collection where i’m sending query.
There are over 40,000 documents in ‘candidates’ collection.
Querying ‘candidates’ collection gives me the error but other collections don’t.
Is there any solution for this problem?
2
Answers
You have to add pagination in your function and use limit in your query because there are too much data.
And if you are using filter with that then set the index in your Firestore Database. You can find more about from here.
The number of candidates in the collection has no impact on the performance of reading a fixed subset of those candidates (unless you’ve added all candidates from the same device you’re querying on, in which case: please uninstall and reinstall the app to clear its local cache).
What does matter is how many candidates your code reads, which is apparently more than your device can keep in memory. You can limit the number of results and or paginate the results.