簡體   English   中英

如何遍歷 firestore 中的所有文檔?

[英]How to iterate over all documents in firestore?

我想遍歷我集合中的所有文檔並獲取一個特定的字段值和 append 到一個列表。

樣本

我如何遍歷所有文檔並將“已報告”的值和 append 獲取到列表 (rep = [])。 在 queryValues() 里面寫什么?

List rep = [];

final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;


void queryValues() {
    
    // what to write here
  }


這是許多方法之一。 首先,您獲得要從中獲取所有文檔的集合。 然后檢查是否為空,然后,您現在可以使用文檔列表更新列表myList

以下面的代碼庫為例:

import "package:cloud_firestore/cloud_firestore.dart";

final FirebaseFirestore firestore = FirebaseFirestore.instance;

void main() {
  List myList = [];
  
  Future<void> queryValues() async {
    // getting all the documents from fb snapshot
    final snapshot = await firestore.collection("collection").get();
    
    // check if the collection is not empty before handling it
    if(snapshot.docs.isNotEmpty) {
      // add all items to myList
      myList.addAll(snapshot.docs);
    }
  }
}

/* **DISCLAIMER** I didn't test the codebase out :) personal */

任何誤解或如果您需要進一步的幫助,請告訴我。

再見!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM