Skip to content

Instantly share code, notes, and snippets.

@marcel-ploch
Created September 11, 2022 17:10
Show Gist options
  • Save marcel-ploch/70f31138cd7dfcaec7d1442e607b967d to your computer and use it in GitHub Desktop.
Save marcel-ploch/70f31138cd7dfcaec7d1442e607b967d to your computer and use it in GitHub Desktop.
Future<int> sumStream(Stream<int> stream) async {
var sum = 0;
await for (final value in stream) {
sum += value;
}
return sum;
}
Stream<int> countStream(int to) async* {
for (int i = 1; i <= to; i++) {
yield i;
}
}
void main() async {
var stream = countStream(2);
var sum = await sumStream(stream);
print(sum); // 55
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment