In Dart, a Stream is like a path where data travels one piece at a time. You can listen to the stream and react when new data arrives. It’s useful when working with things that happen over time, like typing, loading, or network data.
An Empty Stream is a stream that sends no data at all. It does nothing and ends right away. It’s like a quiet river with no water.
You might use an empty stream when there’s nothing to send, but your code still expects a stream. It helps keep things simple and clear when there’s nothing to say.
What Is an Empty Stream?
An Empty Stream is a stream that:
- Sends no values
- Triggers no events except
done
- Emits no errors
- Closes right away when listened to
It’s like turning on a device that immediately powers off — quiet and quick.
In Dart, you can create one like this:
void main() async {
var stream = Stream.empty();
bool empty = await stream.isEmpty;
print(empty); // true
}
This confirms the stream is empty. It does nothing except signal that it’s finished.
How to Use Stream.empty()
When you listen to Stream.empty()
, it doesn’t send any data. It just finishes immediately by sending a done event.
This is useful when your code expects a stream, but there’s nothing to send.
Here’s an example:
void main()async {
var stream = Stream.empty();
stream.listen((data) => print('Data: $data'))
.onDone(() => print('Done!'));
}
As you can see, no data is printed. The stream only signals that it’s finished.
Fun Example – Using Empty Stream in a Game
You can use an empty stream when there’s nothing to show or do, but your code still expects a stream.
In this example, we check if the game has started. If it has, we send some updates. If not, we return an empty stream:
Stream<String> getGameUpdates(bool isGameStarted) {
return isGameStarted
? Stream.fromIterable(['Start!', 'Go!', 'Win!'])
: Stream.empty();
}
void main() {
getGameUpdates(false).listen((data) => print(data))
.onDone(() => print('No updates.'));
}
If isGameStarted
is true
, the stream sends updates. If not, it quietly ends — no extra work needed.
Fun Example – Talking Parrot with No Words
Sometimes, a stream is needed, but there’s nothing to say. An empty stream fits perfectly.
In this example, we create a silent parrot — it listens, but never speaks:
Stream<String> silentParrot() => Stream.empty();
void main() {
silentParrot().listen((word) => print('Parrot says: $word'))
.onDone(() => print('The parrot is done listening.'));
}
The parrot doesn’t say a word. It just listens and ends quietly.
Using Empty Stream with async*
You can also create an empty stream using an async*
function that simply returns no values.
For example:
Stream<int> giveNothing() async* {}
This function returns a stream that immediately closes without sending any data.
You can listen to it like this:
void main() {
giveNothing().listen((value) => print('Value: $value'))
.onDone(() => print('Stream is done.'));
}
This is another simple way to produce an empty stream in Dart.
Conclusion
An empty stream sends no data and ends immediately. It does not emit any events except the done event. This makes it a simple stream that finishes right away.
Empty streams help keep your code clear and easy to follow when there is no data to send, but a stream is still expected. They fit well in many situations where silence is needed.
Even though an empty stream sends nothing, you can still listen to it just like any other stream. It always signals when it is done, allowing your code to respond properly.