Created
July 20, 2020 21:14
-
-
Save rrousselGit/f22cce91e7cb7ceff8118ca68ae0b149 to your computer and use it in GitHub Desktop.
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
void main() async { | |
// Iterate only over the first 5 items | |
await for (final value in counter().take(5)) { | |
print('$value'); | |
} | |
} | |
// Emits an incrementing value every seconds | |
Stream<int> counter() async* { | |
print('start'); | |
try { | |
var i = 0; | |
// While we have an infinite loop, the loop will be | |
// executed only for the first 5 items | |
while (true) { | |
yield i++; | |
await Future.delayed(Duration(seconds: 1)); | |
} | |
} finally { | |
// The finally clause is properly reached when | |
// we stop using the Stream<int> | |
print('end'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment