Skip to content

Instantly share code, notes, and snippets.

@myanmarlinks
Last active July 1, 2019 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myanmarlinks/2db05ab32a5384c4898524be165f7260 to your computer and use it in GitHub Desktop.
Save myanmarlinks/2db05ab32a5384c4898524be165f7260 to your computer and use it in GitHub Desktop.
Dart Basic Part 3
import 'dart:async';
Stream<int> createNumberStream(int last) async* {
for(int i=1; i <= last; i++) {
if(i==5) {
throw new Exception(
"Demo exception when accessing 5th number"
);
}
yield i;
}
}
Future<int> addEvents(Stream<int> stream) async {
var total = 0;
try {
await for (var num in stream) {
total += num;
}
} catch (e) {
return -1;
}
return total;
}
main() async {
var stream = createNumberStream(5);
var subscription = stream.listen(null);
subscription.onData((x) => print("number $x"));
subscription.onError((err) => print("error: $err"));
subscription.onDone(() => print("finished"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment