【问题标题】:Firestore Flutter How to get a list all the documents and its data inside a collection?Firestore Flutter 如何获取集合中所有文档及其数据的列表?
【发布时间】:2020-12-13 05:57:08
【问题描述】:

我一直在努力获取 Firestore 集合中所有文档的列表。我想显示集合中所有文档的所有详细信息。 我的文档树如下询问-

'groups' COLLECTION----->以'groupID'为参考的文档----->'tasks' COLLECTION----->以'taskId'为参考的文档。

现在我想获取特定 groupID 的“任务”集合中的所有文档及其详细信息。

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;
  }

我试过了,但它不起作用,因为“方法 'get' 没有为类型 'CollectionReference' 定义。” 请问如何解决这个问题?

【问题讨论】:

  • 所以,看这个documentation,好像没有get方法,但是从另一个documentation看来,你可以先做一个查询,以便从收藏。我相信这会很有用。

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

只需这样做:

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

我假设您的 MyTask 模型已经有 fromJson 方法,所以就这样做。并将您的 retVal 更改为 List:List&lt;MyTask&gt; retVal = [];。它会获取你所有的文档,并且还会监听集合中是否有变化。

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 2021-07-29
    • 2020-01-22
    • 2019-08-05
    • 2020-09-05
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多