Flutter – yield inside a callback function. Instead of .listen which handles events inside another function you can use await for to handle events inside the outer function.
Separately – you might want to reconsider the pattern when you yield List instances that are still getting populated inside an inner stream callback…
1 2 3 4 5 6 7 8 9 10 11 12 | Stream<List<EventModel>> fetchEvents() async* { final snapshots = Firestore.instance.collection('events').getDocuments().asStream(); await for (final snapshot in snapshots) { // The `await .toList()` ensures the full list is ready // before yielding on the Stream final events = await snapshot.documents .map((document) => EventModel.fromJson(document.data)) .toList(); yield events; } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.