Problem Statement
What does async* function with yield do in Dart?
Explanation
Async* marks a generator function that returns Stream, using yield to emit values asynchronously over time. Each yield pauses the function, emits a value to the stream, and resumes when the next value is requested. This creates streams declaratively without StreamController, perfect for transforming data streams or creating custom stream sources.
For example, Stream<int> countStream() async* { for (int i = 0; i < 10; i++) { await Future.delayed(Duration(seconds: 1)); yield i; } } creates a stream emitting numbers 0-9 with one-second delays. The function executes lazily - code only runs when someone listens to the stream, and pauses between yields when no listeners actively consume values.
Use yield* to yield all values from another stream, incorporating them into your stream. Async* is powerful for transforming streams, creating sequences of async values, or implementing complex async iteration patterns. It's more elegant than manually managing StreamController for many use cases, providing cleaner syntax for stream creation.