Skip to content

Instantly share code, notes, and snippets.

@rrousselGit
Created July 20, 2020 21:14
Show Gist options
  • Save rrousselGit/f22cce91e7cb7ceff8118ca68ae0b149 to your computer and use it in GitHub Desktop.
Save rrousselGit/f22cce91e7cb7ceff8118ca68ae0b149 to your computer and use it in GitHub Desktop.
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