[英]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.