Skip to main content

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’s collection for is a syntactic feature that allows the embedding of loop constructs directly within collection literals (List, Set, and Map). It evaluates the loop during the collection’s instantiation, dynamically generating and appending elements to the enclosing collection based on the loop’s iterations.

Syntax

The collection for supports standard C-style for loops, for-in loops, and asynchronous await for loops. List and Set Literals:
// Standard for loop
[for (<initialization>; <condition>; <update>) <element_expression>]

// For-in loop
{for (<variable> in <iterable>) <element_expression>}
Map Literals:
// Maps require key-value pair expressions
{for (<variable> in <iterable>) <key_expression> : <value_expression>}

Mechanics and Behavior

  • Evaluation: The loop executes at runtime at the exact point the collection literal is evaluated. The <element_expression> is evaluated once per iteration, and the resulting value is immediately appended to the collection in sequential order.
  • Scoping: Variables declared within the for loop header are lexically scoped to the loop body, which in this context is the single expression (or key-value pair) following the loop declaration.
  • Type Inference: The static type of the resulting collection is inferred from the type of the evaluated expression(s). If explicit type arguments are provided to the collection literal (e.g., <String>[]), the evaluated expressions must be statically assignable to that type.
  • Expression vs. Statement: Within a collection literal, for acts as an element generator rather than a control flow statement. It does not support block bodies ({}) or statements like return or break.

Code Examples

Standard for Loop in a List:
final integers = [1, 2, for (var i = 3; i <= 5; i++) i];
// Result: [1, 2, 3, 4, 5]
for-in Loop in a Set:
final source = [1, 2, 3];
final squaredSet = <int>{for (final num in source) num * num};
// Result: {1, 4, 9}
for Loop in a Map:
final keys = ['a', 'b', 'c'];
final charMap = {for (var i = 0; i < keys.length; i++) keys[i]: i};
// Result: {'a': 0, 'b': 1, 'c': 2}

Advanced Composition

Because the collection for yields elements, it can be deeply nested and combined with other collection operators, such as the collection if and the spread operator (...). Nesting and Collection if:
final matrix = [
  for (var i = 0; i < 3; i++)
    for (var j = 0; j < 3; j++)
      if (i == j) 1 else 0
];
// Result: [1, 0, 0, 0, 1, 0, 0, 0, 1]
Asynchronous Iteration (await for): When constructing a collection within an async function, await for can be used to consume a Stream directly inside the collection literal. The collection instantiation will suspend until the stream is fully consumed.
Future<List<int>> consumeStream(Stream<int> stream) async {
  return [
    0, 
    await for (final value in stream) value,
    99
  ];
}
Master Dart with Deep Grasping Methodology!Learn More