TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
yield* (yield-each) statement is a delegation operator used within Dart generator functions (sync* or async*) to emit all values from another Iterable or Stream sequentially. Instead of yielding a single element, yield* unpacks the provided collection or stream and delegates the emission of values to it until it is exhausted.
Syntax
expression: Must evaluate to anIterable<T>when used inside a synchronous generator (sync*), or aStream<T>when used inside an asynchronous generator (async*).
Execution Flow and Mechanics
When the Dart runtime encounters ayield* statement, the following sequence occurs:
- Suspension: The execution of the current (outer) generator is paused.
- Delegation: The runtime begins iterating over the
Iterableor listening to theStreamprovided by theexpression. - Emission: Every value produced by the target
expressionis forwarded directly to the consumer of the outer generator. - Resumption: Once the target
expressionis fully consumed (theIterableis exhausted or theStreamemits a done event), the outer generator resumes execution at the statement immediately following theyield*.
Synchronous Generator Mechanics (sync*)
In a sync* function, yield* expects an Iterable. It acts as a flattened iteration, preventing the need for manual for-in loops to emit nested collections.
Asynchronous Generator Mechanics (async*)
In an async* function, yield* expects a Stream. The outer stream will not emit subsequent values or close until the delegated stream completes.
Type Constraints and Compatibility
The type yielded by the delegatedexpression must match the generic type of the outer generator. Furthermore, Dart enforces strict isolation between synchronous and asynchronous generators:
- You cannot use
yield*on aStreaminside async*function. - You cannot use
yield*on anIterableinside anasync*function directly. To yield anIterableinside anasync*function, it must first be converted to a stream usingStream.fromIterable().
Recursive Optimization
Mechanically,yield* is highly optimized for recursive generators. If a generator calls itself recursively using yield*, Dart flattens the iterator chain. This prevents the creation of deeply nested iterator states, avoiding stack overflow errors and reducing memory overhead that would otherwise occur if elements were manually extracted and yielded one by one.
Master Dart with Deep Grasping Methodology!Learn More





