> ## 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-Each Statement

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

```dart theme={"dark"}
yield* expression;
```

* **`expression`**: Must evaluate to an `Iterable<T>` when used inside a synchronous generator (`sync*`), or a `Stream<T>` when used inside an asynchronous generator (`async*`).

## Execution Flow and Mechanics

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

1. **Suspension**: The execution of the current (outer) generator is paused.
2. **Delegation**: The runtime begins iterating over the `Iterable` or listening to the `Stream` provided by the `expression`.
3. **Emission**: Every value produced by the target `expression` is forwarded directly to the consumer of the outer generator.
4. **Resumption**: Once the target `expression` is fully consumed (the `Iterable` is exhausted or the `Stream` emits a done event), the outer generator resumes execution at the statement immediately following the `yield*`.

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

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

Iterable<int> mainSequence() sync* {
  yield 1;
  yield* subSequence(); // Delegates iteration to subSequence
  yield 4;
}
// Resulting sequence: 1, 2, 3, 4
```

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

```dart theme={"dark"}
Stream<int> subStream() async* {
  yield 2;
  yield 3;
}

Stream<int> mainStream() async* {
  yield 1;
  yield* subStream(); // Delegates event emission to subStream
  yield 4;
}
// Resulting sequence: 1, 2, 3, 4
```

## Type Constraints and Compatibility

The type yielded by the delegated `expression` must match the generic type of the outer generator. Furthermore, Dart enforces strict isolation between synchronous and asynchronous generators:

* You **cannot** use `yield*` on a `Stream` inside a `sync*` function.
* You **cannot** use `yield*` on an `Iterable` inside an `async*` function directly. To yield an `Iterable` inside an `async*` function, it must first be converted to a stream using `Stream.fromIterable()`.

```dart theme={"dark"}
Stream<int> mixedStream(Iterable<int> numbers) async* {
  // yield* numbers; // Compile-time error: expects a Stream
  yield* Stream.fromIterable(numbers); // Valid mechanical conversion
}
```

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

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