简体   繁体   中英

How do i query a collection of firestore that has so many documents without an error in flutter?

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?

You have to add pagination in your function and use limit in your query because there are too much data.

final docs = await FirebaseFirestore.instance
  .collection('candidates')
  .where('name', isEqualTo: name).limit(50)
  .get();

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM