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 statement is a control flow operator used exclusively within generator functions (sync* or async*) to emit a sequence of values one at a time. It suspends the execution of the function, delivers the evaluated expression to the consuming Iterable or Stream, and retains the function’s local state so execution can resume from that exact point upon the next iteration request.
Syntax
Mechanics of yield
When the Dart runtime encounters a yield statement, the following sequence occurs:
- Evaluation: The expression following the
yieldkeyword is evaluated. - Emission: The resulting value is added to the output sequence (
Iterablefor synchronous generators,Streamfor asynchronous generators). - Suspension: The execution of the generator function is paused immediately after the
yieldstatement. All local variables and execution state are preserved in memory. - Resumption: The function remains suspended until the consumer requests the next value (e.g., via
Iterator.moveNext()for anIterable, or when theStreamSubscriptionis ready for the next event). Once requested, execution resumes at the statement immediately following theyield.
Synchronous vs. Asynchronous Contexts
The behavior ofyield adapts based on the generator’s modifier:
Synchronous Generator (sync*)
Emits values to an Iterable<T>. The suspension is driven by the synchronous pulling of values via an Iterator.
async*)
Emits values to a Stream<T>. The suspension is driven by the asynchronous event loop and the backpressure of the stream’s subscription.
The yield* (Yield-Each) Operator
The yield* statement is a delegation operator. Instead of emitting a single value, it delegates the emission process to another Iterable or Stream.
When yield* is invoked, the current generator pauses and forwards all values from the target collection to the consumer. The current generator only resumes execution once the delegated Iterable or Stream is completely exhausted.
Technical Constraints
yieldandyield*result in a compile-time error if used outside of functions marked withsync*orasync*.- A
returnstatement inside a generator function terminates the sequence, closing the underlyingIterableorStream. It does not emit a value. - In an
async*function,yield*must delegate to aStream, whereas in async*function, it must delegate to anIterable.
Master Dart with Deep Grasping Methodology!Learn More





