![](/img/trans.png)
[英]In Firestore, how do I change for all documents of a collection of which a certain field has a certain value, that field in a certain value?
[英]How do i query a collection of firestore that has so many documents without an error in flutter?
我正在嘗試從 firebase firestore 獲取一些數據。
下面是我用來感染數據的代碼
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();
}
但是,每當我調用此函數時,應用程序都會在查詢時崩潰。
以下是應用程序崩潰前的調試控制台消息
E/AndroidRuntime(25623): java.lang.OutOfMemoryError: Firestore (24.1.2) 內存不足。 檢查您的查詢以確保它們沒有加載過多的數據。
我認為問題在於我發送查詢的集合的大小。
“候選人”集合中有超過 40,000 份文檔。
查詢“候選人”集合會給我錯誤,但其他集合不會。
這個問題有什么解決辦法嗎?
您必須在函數中添加分頁並在查詢中使用限制,因為數據太多。
final docs = await FirebaseFirestore.instance
.collection('candidates')
.where('name', isEqualTo: name).limit(50)
.get();
如果您使用過濾器,那么在您的 Firestore 數據庫中設置索引。 你可以從這里找到更多關於。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.