Listening With await for
Read events as they arrive.
Listening With await for is a free Dart Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What a Stream Is
A Stream is a sequence of values that arrive over time, not all at once. Think of it as a Future that can deliver many results instead of just one.
Streams vs Futures
A Future gives you one value later. A Stream hands you a whole series of events, one after another, until it tells you it is done. 🌊
Meet await for
The await for loop reads events from a stream as they arrive. Each pass through the loop gives you the next value the stream emits.
await for (final value in stream) {
print(value);
}It Must Live in async
Because await for pauses to wait for events, it only works inside a function marked async. The function suspends until the next value is ready.
Future<void> show(Stream<int> s) async {
await for (final n in s) print(n);
}A Stream to Listen To
You can make a quick stream with Stream.fromIterable. It emits each element of the list in order, perfect for trying await for.
final stream = Stream.fromIterable([1, 2, 3]);Looping the Events
Pair that stream with await for and your loop runs once per emitted value. Here it prints 1, then 2, then 3.
await for (final n in Stream.fromIterable([1, 2, 3])) {
print(n);
}Waiting Between Events
Real streams often pause between events. With await for, your code simply waits during the gaps and resumes the instant the next value comes in. ⏳
final ticks = Stream.periodic(
Duration(seconds: 1), (i) => i);The Loop Ends Naturally
When a stream finishes sending events, it sends a done signal. Your await for loop exits on its own, and the function continues after it.
await for (final n in finiteStream) print(n);
print('Stream finished');Errors Reach the Loop
If the stream emits an error, that error is thrown right at the await for line. You can wrap the loop in try/catch to handle it gracefully.
try {
await for (final n in maybeFails) print(n);
} catch (e) {
print('Caught: $e');
}break Cancels Listening
Using break inside await for stops the loop and quietly cancels your subscription to the stream. You no longer receive its remaining events.
await for (final n in stream) {
if (n > 100) break;
}One Listener at a Time
An await for loop subscribes to a single-subscription stream. That stream can be read only once, so a second loop over it would throw an error.
Quick Check
Test what you know about await for.
Recap
You learned that a Stream delivers many values over time, and that await for reads them one by one inside an async function until done. Nicely done! 🎉
Frequently asked questions
Is the “Listening With await for” lesson free?
Yes — the full text of “Listening With await for” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Listening With await for”?
Read events as they arrive. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Listening With await for” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Listening With await for
- Single vs Broadcast Streams
- Transforming Streams: map, where, take
- Creating Streams With async*