Skip to content

Instantly share code, notes, and snippets.

@dicenull
Created March 14, 2024 08:16
Show Gist options
  • Save dicenull/47f32080c744fef661cd5e486728db59 to your computer and use it in GitHub Desktop.
Save dicenull/47f32080c744fef661cd5e486728db59 to your computer and use it in GitHub Desktop.
class Spacecraft {
final String name;
Spacecraft(this.name);
}
const oneSecond = Duration(seconds: 1);
// ···
Future<void> printWithDelay(String message) async {
await Future.delayed(oneSecond);
print(message);
}
Stream<String> report(Spacecraft craft, Iterable<String> objects) async* {
for (final object in objects) {
await Future.delayed(oneSecond);
yield '${craft.name} flies by $object';
}
}
void main() async {
printWithDelay('hello.');
const flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune'];
final reportStream = report(Spacecraft('Voyager I'), flybyObjects);
await for (final message in reportStream) {
print(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment