【发布时间】:2019-09-20 05:39:45
【问题描述】:
我需要为一个函数生成一个列表;但是,我想从回调函数中生成列表,该回调函数本身位于主函数内部 - 这导致 yield 语句不是为主函数执行,而是为回调函数执行。
我的问题与这里解决的问题非常相似:Dart Component: How to return result of asynchronous callback? 但我不能使用 Completer,因为我需要让步而不是返回。
下面的代码应该能更好地描述问题:
Stream<List<EventModel>> fetchEvents() async* { //function [1]
Firestore.instance
.collection('events')
.getDocuments()
.asStream()
.listen((snapshot) async* { //function [2]
List<EventModel> list = List();
snapshot.documents.forEach((document) {
list.add(EventModel.fromJson(document.data));
});
yield list; //This is where my problem lies - I need to yield for function [1] not [2]
});
}
【问题讨论】:
标签: dart flutter google-cloud-firestore dart-async