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

The `for-in` loop in Dart is a control flow statement designed for sequential iteration over objects that implement the `Iterable` interface. It abstracts the underlying `Iterator` mechanics, providing a concise syntax to access each element in a collection sequentially without manual index management.

## Syntax

```dart theme={"dark"}
for (<declaration|identifier> in iterable) {
  // Statement(s) to execute
}
```

* **`declaration|identifier`**: Either a new variable declaration (scoped to the loop body) or an existing identifier (externally scoped) that receives the current element in the iteration.
* **`iterable`**: An expression that evaluates to a Dart `Iterable`.

## Underlying Mechanics

When a `for-in` loop executes, Dart performs the following operations under the hood:

1. Evaluates the `iterable` expression.
2. Calls the `.iterator` getter on the resulting `Iterable` to obtain an `Iterator` object.
3. Enters a loop that calls `moveNext()` on the `Iterator`.
4. If `moveNext()` returns `true`, it assigns the value of `Iterator.current` to the loop variable and executes the loop body.
5. If `moveNext()` returns `false`, the loop terminates.

## Variable Binding and Scoping

The loop variable can be newly declared within the loop construct or it can reference an existing, externally scoped variable.

```dart theme={"dark"}
Iterable<String> tokens = ['alpha', 'beta', 'gamma'];

// Explicit typing (scoped to the loop body)
for (String token in tokens) {
  print(token);
}

// Implicit typing (type inferred as String)
for (var token in tokens) {
  print(token);
}

// Immutable binding (prevents reassignment within the loop body)
for (final token in tokens) {
  print(token);
}

// Assignment to an existing, externally scoped variable
String currentToken = '';
for (currentToken in tokens) {
  print(currentToken);
}
// currentToken retains the final iteration value ('gamma') after termination
```

## Pattern Matching Integration

As of Dart 3.0, the `for-in` loop supports destructuring patterns directly within the declaration clause. This allows for unpacking complex data structures, such as Records or nested collections, during iteration.

```dart theme={"dark"}
Iterable<(int, String)> records = [(1, 'A'), (2, 'B')];

// Destructuring a Record into separate variables
for (final (id, name) in records) {
  print('$id: $name');
}
```

## Technical Constraints

* **No Index Access**: The `for-in` loop does not expose an iteration counter or index. If an index is required, developers must use a standard `for` loop or the `.indexed` getter (e.g., `for (final (index, item) in items.indexed)`).
* **Concurrent Modification**: The `for-in` language construct itself does not enforce structural immutability; it merely invokes `moveNext()`. However, the specific `Iterator` implementations of standard library collections (such as `List` and `Set`) are designed to be fail-fast. Attempting to modify the length or structure of these standard collections during iteration will cause their iterators to throw a `ConcurrentModificationError`. Custom `Iterable` implementations dictate their own modification rules and may allow mutations without throwing an error.
* **Synchronous Only**: The standard `for-in` loop operates strictly on synchronous `Iterable` objects. To iterate over asynchronous data sequences (`Stream`), Dart provides the `await for` variant.

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