Created
March 14, 2024 08:16
-
-
Save dicenull/47f32080c744fef661cd5e486728db59 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
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