如何转换Stream< QuerySnapshot>列出< myObject>
我正在查询Firestore,并将流作为QuerySnapshots流重新获得。我需要将流中包含的文档映射到对象列表。
I'm querying Firestore and getting a Stream back as a Stream of QuerySnapshots. I need to map the included Documents in the stream to a List of objects.
下面的代码不起作用(显然)……也许我只是在寻找这完全是错误的。
The code below doesn't work (obviously)...maybe I'm just looking at this entirely wrong.
List<UserTask> getUserTaskList() {
List<UserTask> list;
Stream<QuerySnapshot> stream =
Firestore.instance.collection('userTasks').snapshots();
stream.listen((snapshot) {
snapshot.documents.forEach((doc) {
UserTask userTask = UserTask(
doc.data['id'],
doc.data['Description'],
etc...);
list.add(userTask);
});
});
return list;
}
使用上面的代码,因为它不等待整个流(或其中的任何一个),列表始终返回为null。简而言之,如何将我的流转换为列表?
With the code above, since it doesn't wait for the entire stream (or any of it actually), list is always returned as null. In short, how do I convert my stream to a List?
注意:我对Dart领域很陌生,所以请放轻松:)
Note: I'm pretty new to the world of Dart, so go easy on me :)
谢谢!
首先,请考虑以下事项:此函数必须很快返回。所有功能都可以,否则UI会挂起。但是,您希望函数返回来自互联网的内容。这需要时间。该函数必须返回。函数无法简单地进行网络请求并返回结果。欢迎来到异步编程的世界。
First of all, think about this: this function has to return very quickly. All functions do, otherwise UI would hang. However, you are expecting the function to return something that comes from the internet. It takes time. The function has to return. There is no way for a function to simply do a network request and return you the result. Welcome to the world of asynchronous programming.
此外,您拥有的流不是 DocumentSnapshot
s的流(您可以将其转换为 UserTask
s),但流 QuerySnapshot
s(您可以将其转换为 List< UserTask>
s)。注意那里的复数。如果您只想一次获取所有 UserTask
,则应该使用 Future
而不是流
。如果要在每次更改后重复获取所有 UserTask
,则可以使用 Stream
。
Furthermore, the stream you have is not a stream of DocumentSnapshot
s (which you can convert to UserTask
s), but a stream of QuerySnapshot
s (which you can convert to List<UserTask>
s). Notice the plural there. If you simply want to get all your UserTask
s once, you should have a Future
instead of a Stream
. If you want to repeatedly get all your UserTask
s after each change, then using a Stream
makes sense.
由于您说过要获得 List< UserTask>
,所以我假设您想获得 UserTask
的集合仅一次。
Since you said you want to get a List<UserTask>
, I'm assuming you want to get the collection of UserTask
s only once.
这就是您的代码的含义:
Here's what your code becomes in this light:
Future<List<UserTask>> getUserTaskList() async {
QuerySnapshot qShot =
await Firestore.instance.collection('userTasks').getDocuments();
return qShot.documents.map(
(doc) => UserTask(
doc.data['id'],
doc.data['Description'],
etc...)
).toList();
}
main() async {
List<UserTask> tasks = await getUserTaskList();
useTasklist(tasks); // yay, the list is here
}
现在,如果您真的要使用流,请按以下步骤操作:
Now if you really wanted to use a stream, here's how you could do it:
Stream<List<UserTask>> getUserTaskLists() async {
Stream<QuerySnapshot> stream =
Firestore.instance.collection('userTasks').snapshots();
return stream.map(
(qShot) => qShot.documents.map(
(doc) => UserTask(
doc.data['id'],
doc.data['Description'],
etc...)
).toList()
);
}
main() async {
await for (List<UserTask> tasks in getUserTaskLists()) {
useTasklist(tasks); // yay, the NEXT list is here
}
}
希望有帮助。