簡體   English   中英

Flutter:如何在 Firestore 中獲取集合的所有文檔名稱

[英]Flutter: How can I get all documents name of a collection in Firestore

我現在使用 Firestore 在 Flutter 中制作了一個應用程序,我將瀏覽集合中的所有文檔,並且我想獲取文檔名稱(id)和文檔的字段並對其進行處理。 我已經制作了一個顯示數據的列表視圖,但我不能用它做一些事情,例如,將它添加到列表或其他東西中。 謝謝

您可以通過獲取以下文件列表來做到這一點:

final QuerySnapshot result =
          await Firestore.instance.collection('myCollection').getDocuments();
final List<DocumentSnapshot> documents = result.documents;

之后,您可以迭代此列表並獲取數據:

documents.forEach((data) => print(data));

一、做Firestore的引用

final userRef = Firestore.instance.collection('users');

二、制作迭代器從fireStore獲取所有文檔

 userRef.getDocuments().then((snapshot) {
      snapshot.documents.forEach((doc) {
        print(doc.data);
      });
    });
  • 您可以獲取特定數據,例如電子郵件:
print(doc.data['email'];
  • 您可以通過以下方式獲取文檔 ID
print(doc.documentID);
  • 可以通過以下方式檢查文檔是否存在
print(doc.exists);

這是一個更新的答案。 最近getDocuments在 Flutter 中被 firebase 棄用。

取而代之的是,我們可以使用get來查詢集合中的所有文檔。

像這樣

final QuerySnapshot result =
      await Firestore.instance.collection('myCollection').get();
final List<DocumentSnapshot> documents = result.docs;

並列出所有文檔,您只需要像這樣循環文檔列表

documents.forEach((DOC) => print(DOC.data()));

正如Shubham所說的getDocuments它現在已被棄用,所以如果你只想返回文件的名稱,你應該這樣做:

final db = FirebaseFirestore.instance;
var result=await db.collection('Pets').get();
result.docs.forEach((res) {print(res.id); });

您也可以where如下方式打印特定的:

var result=await db.collection('Pets').where("sex",whereIn:["Male"] ).get();
result.docs.forEach((res) {print(res.id); });

這對我有用,祝你好運。

您可以通過使用流偵聽數據來有效地檢索數據。

首先獲取 Firestore 的參考:

final userRef = Firestore.instance.collection('users');

使用以下功能,您將訂閱來自 Firestore 的數據。 這意味着每次在您的集合中進行修改時,Firestore 都會自動通知您的應用程序(您正在偵聽更改的代碼將在您的應用程序每次收到通知時重新運行)。

在第一個 for 循環中,您正在偵聽集合中的更改,循環遍歷快照。 第二個 for 循環將遍歷List<QueryDocumentSnapshot> ,因此您可以訪問每個快照的文檔。

void getData() async {
 await for (var snapshot in userRef.snapshots()) {
  for(var document in snapshot.docs()) {
    print(document.data()); //You'll get printed the whole data
    print(document['email']); //You'll get printed the email field.
    print(document.id); //You'll get printed the document ID
  }
 }

提示:您可以直接在 Firebase 網站上更改收藏中的某些內容來測試流式傳輸。

您可以使用

    firestore.instance.collection('myCollection').getDocuments();

2021 年之前,但現在您應該提及如下:

     final querySnap = await firestore.instance.collection('myCollection').get();


    for (var i = 0; i < querySnap.docs.length; i++) {
        final data = querySnap.docs[i].id;
        log(data.toString());
    }

您還可以通過使用FutureBuilder包裝小部件來獲取文檔名稱,如下所示:

FutureBuilder(
              future: FirebaseFirestore.instance
                  .collection("collection_name")
                  .get(),
              builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasData) {
                  QuerySnapshot documents = snapshot.data;
                  List<DocumentSnapshot> docs = documents.docs;
                  docs.forEach((data) {
                    print(data.id);
                  });
                } else {
                  print("nodata");
                }
                return Container();
              })

暫無
暫無

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

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