> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dart Yield Statement

The `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

```dart theme={"dark"}
// Emits a single value
yield expression;

// Delegates to another Iterable or Stream
yield* iterableOrStreamExpression;
```

## Mechanics of `yield`

When the Dart runtime encounters a `yield` statement, the following sequence occurs:

1. **Evaluation:** The expression following the `yield` keyword is evaluated.
2. **Emission:** The resulting value is added to the output sequence (`Iterable` for synchronous generators, `Stream` for asynchronous generators).
3. **Suspension:** The execution of the generator function is paused immediately after the `yield` statement. All local variables and execution state are preserved in memory.
4. **Resumption:** The function remains suspended until the consumer requests the next value (e.g., via `Iterator.moveNext()` for an `Iterable`, or when the `StreamSubscription` is ready for the next event). Once requested, execution resumes at the statement immediately following the `yield`.

## Synchronous vs. Asynchronous Contexts

The behavior of `yield` 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`.

```dart theme={"dark"}
Iterable<int> generateSynchronous() sync* {
  yield 1; // Pauses until iterator.moveNext() is called
  yield 2;
}
```

**Asynchronous Generator (`async*`)**
Emits values to a `Stream<T>`. The suspension is driven by the asynchronous event loop and the backpressure of the stream's subscription.

```dart theme={"dark"}
Stream<int> generateAsynchronous() async* {
  yield 1; // Emits to the stream, pauses until the stream is ready
  yield 2;
}
```

## 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.

```dart theme={"dark"}
Iterable<int> subSequence() sync* {
  yield 2;
  yield 3;
}

Iterable<int> mainSequence() sync* {
  yield 1;
  yield* subSequence(); // Flattens the subSequence into the current Iterable
  yield 4;
}
// Resulting sequence: 1, 2, 3, 4
```

## Technical Constraints

* `yield` and `yield*` result in a compile-time error if used outside of functions marked with `sync*` or `async*`.
* A `return` statement inside a generator function terminates the sequence, closing the underlying `Iterable` or `Stream`. It does not emit a value.
* In an `async*` function, `yield*` must delegate to a `Stream`, whereas in a `sync*` function, it must delegate to an `Iterable`.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
