Created
September 4, 2019 08:49
-
-
Save mkustermann/0ec6468402878d34f363e7036e073a0f to your computer and use it in GitHub Desktop.
Use await for with broadcast stream and return
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
main() async { | |
print('invoking bar'); | |
await bar(); | |
print('bar returned'); | |
} | |
Future bar() async { | |
int i = 0; | |
await for (var item in stream()) { | |
print(item); | |
if (i++ == 5) { | |
return; | |
} | |
} | |
} | |
Stream stream() { | |
StreamController s; | |
s = StreamController.broadcast(onListen: () async { | |
int i = 0; | |
emit() { | |
s.add(i++); | |
Timer.run(emit); | |
} | |
emit(); | |
}); | |
return s.stream; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment