Created
March 9, 2020 09:13
-
-
Save AlexKenbo/e1e51dae93945e576c737cfffe41f204 to your computer and use it in GitHub Desktop.
Error and simple Stream
This file contains hidden or 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'; | |
Future<int> sumStream(Stream<int> stream) async { | |
var sum = 0; | |
try { | |
await for (var value in stream) { | |
sum += value; | |
} | |
} catch (e) { | |
return -1; | |
} | |
return sum; | |
} | |
Stream<int> countStream(int to) async* { | |
for (int i = 1; i <= to; i++) { | |
if (i == 4) { | |
throw new Exception('Intentional exception'); | |
} else { | |
yield i; | |
} | |
} | |
} | |
main() async { | |
var stream = countStream(10); | |
var sum = await sumStream(stream); | |
print(sum); // -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment