Single vs Broadcast Streams
Choose the right stream for many listeners.
Single vs Broadcast Streams is a free Dart Academy lesson on CoddyKit — lesson 2 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.
Two Kinds of Streams
Dart streams come in two flavors: single-subscription and broadcast. Picking the right one depends on how many listeners you need.
Single-Subscription Streams
A single-subscription stream allows exactly one listener for its lifetime. It is the default and is ideal for a one-time sequence like reading a file. 📄
Listen Once Only
Try to listen to a single-subscription stream twice and Dart throws a StateError. The stream remembers it already has its one listener.
final s = Stream.fromIterable([1, 2]);
s.listen(print);
s.listen(print); // throws StateErrorBuffered Until You Listen
A single-subscription stream waits patiently. It does not produce events until something starts listening, so no value is ever missed.
Broadcast Streams
A broadcast stream lets many listeners subscribe at the same time. Each active listener receives the same events as they fire. 📡
Convert to Broadcast
Turn a single-subscription stream into a shared one by calling asBroadcastStream. Now several listeners can attach to it.
final shared = source.asBroadcastStream();
shared.listen(print);
shared.listen((v) => log(v));Late Listeners Miss Events
Broadcast streams do not buffer. A listener that subscribes late only sees events emitted from that moment on, never the earlier ones.
Controllers Choose the Type
A StreamController creates a single-subscription stream by default. Use the broadcast constructor when you want multiple listeners instead.
final ctrl = StreamController<int>.broadcast();
ctrl.stream.listen(print);When to Use Single
Reach for a single-subscription stream when data flows once to one consumer, such as the bytes of a network response or a file read.
When to Use Broadcast
Choose a broadcast stream for ongoing events that many parts of your app care about, like button taps or live sensor updates.
Check Before Listening
Not sure which type you have? The isBroadcast property tells you, so you can avoid the double-listen StateError on single streams.
if (!stream.isBroadcast) {
// only one listener allowed
}Quick Check
Which stream fits many listeners?
Recap
Single-subscription streams serve one listener and buffer until read; broadcast streams serve many but skip past events. Pick by listener count. 🎉
Frequently asked questions
Is the “Single vs Broadcast Streams” lesson free?
Yes — the full text of “Single vs Broadcast Streams” 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 “Single vs Broadcast Streams”?
Choose the right stream for many listeners. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Single vs Broadcast Streams” 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*