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

An `Iterable` in Dart is an abstract class representing a sequence of elements that can be accessed sequentially. It serves as the foundational interface for collections like `List` and `Set`, defining the contract for traversing elements without exposing the underlying data structure's implementation details.

## The Iterator Mechanism

An `Iterable` itself does not maintain iteration state. Instead, it provides an `iterator` getter that returns an `Iterator` object. The `Iterator` is responsible for maintaining the cursor position and traversing the sequence using `moveNext()` and `current`.

```dart theme={"dark"}
Iterable<int> numbers = [1, 2, 3];
Iterator<int> iterator = numbers.iterator;

while (iterator.moveNext()) {
  print(iterator.current);
}
```

## Lazy Evaluation

A defining characteristic of Dart's `Iterable` methods (such as `map`, `where`, `expand`, and `take`) is lazy evaluation. Invoking these methods does not immediately process the elements or allocate memory for a new collection. Instead, they return a new `Iterable` that wraps the original one. The computation is deferred until the elements are explicitly consumed (e.g., via a `for-in` loop, `.toList()`, or `.length`).

```dart theme={"dark"}
Iterable<int> baseIterable = [1, 2, 3, 4];

// No computation happens here. Returns a MappedIterable.
Iterable<int> lazyMapped = baseIterable.map((n) {
  print('Processing $n');
  return n * 2;
});

// Computation is triggered here during consumption.
List<int> materializedList = lazyMapped.toList(); 
```

## Synchronous Generators

Dart provides language-level support for constructing custom `Iterable` sequences using synchronous generator functions. By marking a function with `sync*` and using the `yield` keyword, the Dart compiler automatically generates the underlying `Iterable` and `Iterator` boilerplate.

```dart theme={"dark"}
Iterable<int> generateSequence(int max) sync* {
  for (int i = 0; i < max; i++) {
    yield i; // Emits a single value to the Iterable
  }
}

Iterable<int> recursiveSequence(int max) sync* {
  if (max > 0) {
    yield max;
    yield* recursiveSequence(max - 1); // Delegates to another Iterable
  }
}
```

## Structural Characteristics

1. **Immutability of Interface:** The base `Iterable<E>` class exposes a strictly read-only view. It defines accessors (`first`, `last`, `elementAt`) and transformation methods, but lacks mutator methods (`add`, `remove`). Mutation is strictly the domain of concrete subclasses like `List<E>`.
2. **Iteration Behavior:** The Dart `Iterable` contract explicitly requires that iterables can be iterated multiple times. Each time the `.iterator` getter is accessed, it must return a new, independent `Iterator` that starts from the beginning of the sequence. However, while multiple passes are guaranteed to be possible, the elements yielded—or their order—are not strictly required to be identical across iterations if the `Iterable` is backed by impure functions, randomized logic, or changing external state (such as those created via `Iterable.generate` or `sync*`).
3. **Length Computation:** Because iterables can be lazily generated or infinite, accessing the `.length` property on an unmaterialized `Iterable` operates in $O(n)$ time, requiring a full traversal of the sequence to count the elements, unless overridden by a concrete subclass (like `List`) to operate in $O(1)$ time.

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