A single data stream is a stream that sends just one piece of data and then closes. Unlike streams that send many values over time, this one sends only one event.
You might use a single data stream when you want to deliver a simple message or result in a way that fits with other stream-based code. It keeps your program consistent by using streams even for small tasks.
Using a stream for a single value allows easy integration with other stream operations, making your code neat and flexible.
Creating a Single Data Stream
You can create a single data stream easily using Stream.value()
. This sends one value and then closes the stream.
Here’s an example that sends a simple greeting:
Stream<String> greet() => Stream.value('Hello, Dart!');
void main() {
greet().listen((message) {
print(message);
});
}
This stream sends just one message and then ends.
Listening to a Single Data Stream
To get the value from a single data stream, you listen to it like any other stream. The listener receives the value when it arrives.
Here’s how to listen and print the greeting from the greet
stream:
Stream<String> greet() => Stream.value('Hello, Dart!');
void main() {
greet().listen((message) {
print(message);
});
}
Listening lets your code react when the single value is sent.
Using async*
to Yield One Value
You can create a single data stream using an async*
function with the yield
keyword. This sends one value and then closes the stream.
Here’s an example that yields a random animal name:
Stream<String> randomAnimal() async* {
yield 'Elephant';
}
void main() {
randomAnimal().listen((animal) {
print('Random animal: $animal');
});
}
This method lets you write streams with more flexibility using asynchronous code.
Single Data Stream with Delay
You can add a delay before sending the value in a single data stream by using await
inside an async*
function.
Here’s an example that sends a cheer after one second:
Stream<String> delayedCheer() async* {
await Future.delayed(Duration(seconds: 1));
yield 'Go, team!';
}
void main() {
delayedCheer().listen((cheer) {
print(cheer);
});
}
This lets you control when the single value is sent.
Using first
to Get Single Data from Any Stream
Sometimes, a stream sends many values, but you only want the very first one. Dart lets you get that value using the first
property, which returns a Future
with the first item.
Here’s an example that gets the first chess move from a stream of moves:
Future<String> firstMove(Stream<String> moves) => moves.first;
void main() async {
var moves = Stream.fromIterable(['e4', 'd4', 'Nf3']);
String move = await firstMove(moves);
print('First move: $move');
}
This way, you can quickly get a single value from any stream.
Conclusion
Single data streams send just one value and then close. You can create them easily using Stream.value()
or with async*
and yield
.
Listening to these streams works just like with any other stream. You can also add delays or get the first value from any stream using first
.
Single data streams keep your code simple and consistent when working with one-time events or messages.