Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Async Generators example
import 'dart:async';
void main() {
// asynchronousNaturalsTo(10).listen(print);
final controller = asyncNaturalsTo(10);
controller.stream.listen((n) => print('YEss $n'),
onDone: () {
controller.close();
});
}
Stream<int> asynchronousNaturalsTo(int n) async* {
int k = 0;
while (k < n) yield k++;
}
StreamController<int> asyncNaturalsTo(int n) {
final controller = StreamController<int>();
int k = 0;
while (k < n) {
controller.add(k);
k++;
}
return controller;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment