简体   繁体   English

Firestore Flutter 如何获取集合中所有文档及其数据的列表?

[英]Firestore Flutter How to get a list all the documents and its data inside a collection?

I have been working to get a list of all the documents inside a firestore collection.我一直在努力获取 firestore 集合中所有文档的列表。 I want to display all details of all documents inside a collection.我想显示集合中所有文档的所有详细信息。 My document tree is ask follows-我的文档树问如下-

'groups' COLLECTION----->Documents w 'groupID' as reference------>'tasks' COLLECTION------>Documents w 'taskId' as reference. 'groups' COLLECTION---->文档 w 'groupID' 作为参考------>'tasks' COLLECTION------>文档 w 'taskId' 作为参考。

Now I want to get all documents and its details inside 'tasks' collection for a particular groupID.现在我想获取特定 groupID 的“tasks”集合中的所有文档及其详细信息。

Future<MyTask> getCurrentTask(String groupId) async {
    MyTask retVal = MyTask();
    try {
      DocumentSnapshot _docSnapshot =
          await _firestore.collection("groups").document(groupId).collection("tasks").get();
      retVal.taskId = taskId;
      retVal.taskName = _docSnapshot.data['taskName'];
      retVal.dueTime = _docSnapshot.data['dueTime'];
      retVal.member =_docSnapshot.data['member'];
      retVal.completed = _docSnapshot.data['completed'];
  
    } catch (e) {
      print(e);
    }
    return retVal;
  }

I tried this but it doesnt work as "The method 'get' isn't defined for the type 'CollectionReference'."我试过这个,但它不起作用,因为“方法 'get' 没有为类型 'CollectionReference' 定义。” How to get around this please?请问如何解决这个问题?

Simply do like this:只需这样做:

Firestore.instance.collection("groups").document(groupID).collection("task").snapshots().listen((event) {
  retVal = event.documents.map((e) => MyTask.fromJson(e.data)).toList();
});

I assume your MyTask model already have fromJson method so do it like that.我假设你的 MyTask 模型已经有 fromJson 方法,所以这样做。 And change your retVal to List: List<MyTask> retVal = [];并将您的 retVal 更改为 List: List<MyTask> retVal = []; . . It will get all of your document and also listen whether there's change's on the collection.它将获取您的所有文档,并侦听集合中是否有更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Flutter 删除 Firestore 中集合中的所有文档 - How to Delete all documents in collection in Firestore with Flutter 如何获取集合中的文档列表,而不获取firebase cloud firestore中文档中的所有字段? - How to get a list of documents in a collection, without taking all the fields in the documents in firebase cloud firestore? 如何获取 Firestore 集合中所有文档的子集合的特定文档? - How to get specific Documents of a Subcollection of all Documents in a Collection in Firestore? 如何在 Firestore -Flutter 中将所有文档从一个集合复制到另一个集合? - How to copy all documents from one collection to other in Firestore -Flutter? 如何从 firestore 获取文档集合并将其转换为 flutter 中的列表? - How do you get a a collection of documents from firestore and turn it into a list in flutter? FireStore Swift 4:如何获取集合中所有文档的总数,并获取每个文档的详细信息? - FireStore Swift 4 : How to get total count of all the documents inside a collection, and get details of each document? 在 Flutter 中的嵌套集合中查询 Firestore 文档 - Query Firestore documents inside a nested collection in Flutter 使用 Flutter 中的 Firestore 获取不同集合中的文档列表 - Get list of documents inside different collections with Firestore in Flutter Firestore 如何在文档列表中获取顶级集合? - Firestore how can i get top-level collection inside documents list? 从集合 firebase firestore 中的所有文档中获取数据 - get data from all documents in an collection firebase firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM