繁体   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