Last active
July 1, 2019 15:14
-
-
Save myanmarlinks/2db05ab32a5384c4898524be165f7260 to your computer and use it in GitHub Desktop.
Dart Basic Part 3
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'; | |
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