> ## 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 Await For Loop

The `await for` loop is an asynchronous iteration construct in Dart used to consume events from a `Stream`. It suspends the execution of the enclosing `async` function until the stream emits a data event, executes the loop body with that event, and continues this process until the stream is closed.

## Syntax

```dart theme={"dark"}
await for (variableDeclaration in streamExpression) {
  // Executes for each data event emitted by the stream
}
```

## Execution Mechanics

To utilize an `await for` loop, the enclosing function must be marked with the `async` or `async*` modifier. The loop operates through the following lifecycle:

1. **Subscription:** The loop implicitly subscribes to the provided `Stream`.
2. **Suspension:** The execution of the enclosing function is suspended, yielding control back to the Dart event loop while waiting for the stream to emit an event.
3. **Execution:** When a data event is emitted, the variable is bound to the event data, and the loop body executes.
4. **Iteration:** Steps 2 and 3 repeat for every subsequent event.
5. **Termination:** The loop completes naturally, and execution proceeds to the next statement, only when the stream emits a "done" event indicating it is closed.

## Premature Termination and Cancellation

You can interrupt an `await for` loop using standard control flow statements such as `break` or `return`. When the loop is exited prematurely, Dart automatically cancels the underlying stream subscription, preventing memory leaks and stopping further event generation if the stream is lazy.

## Error Handling

If the stream emits an error event, the `await for` loop throws that error synchronously within the asynchronous context. The loop terminates immediately, and the subscription is canceled. These errors can be intercepted by wrapping the `await for` loop in a standard `try-catch` block.

## Code Example

```dart theme={"dark"}
Stream<int> generateStream() async* {
  yield 1;
  yield 2;
  // Stream closes automatically after the last yield
}

Future<void> processStream() async {
  try {
    await for (final int event in generateStream()) {
      print(event);
      
      if (event == 2) {
        break; // Exits the loop and cancels the stream subscription
      }
    }
  } catch (e) {
    // Catches any error events emitted by the stream
    print('Caught stream error: $e');
  }
}
```

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