Given:
Stream<String> stream = new Stream<String>.fromIterable(['mene', 'mene', 'tekel', 'parsin']);
then
print('BEFORE'); stream.listen((s) { print(s); }); print('AFTER');
gives:
BEFORE AFTER mene mene tekel parsin
then:
print('BEFORE'); await for(String s in stream) { print(s); } print('AFTER');
gives:
BEFORE mene mene tekel parsin AFTER
stream.listen() sets the code that will be placed in the event queue when an event arrives, then the following code is executed.
await for pauses between events and continues to do so until the thread is executed, so subsequent code will not be executed until this happens.
I use wait when I have a thread that I know will have final events and I need to process them before doing anything else (essentially as if I were dealing with a list of futures).
Check out the https://www.dartlang.org/articles/language/beyond-async await for description.
source share