Introduction
Collection for is a Dart feature that lets you generate multiple elements inside a collection literal using a for loop, all in a single expression. It is a concise alternative to building collections with map() or traditional loop-and-add patterns.
Why It Matters
Generating collections from sequences — like turning a list of strings into a list of widgets, or creating a numbered list — is extremely common. Collection for keeps this inline and readable without needing temporary variables or helper functions.
Syntax
var list = [
for (var item in iterable) transform(item),
];
var list2 = [
for (int i = start; i < end; i++) expression,
];
Code Example
void main() {
// Generate squares 1–5
var squares = [for (int i = 1; i <= 5; i++) i * i];
print(squares); // [1, 4, 9, 16, 25]
// Transform strings to uppercase
var fruits = ['apple', 'banana', 'cherry'];
var upper = [for (var f in fruits) f.toUpperCase()];
print(upper); // [APPLE, BANANA, CHERRY]
// Generate a Map from a list
var names = ['Alice', 'Bob', 'Carol'];
var nameMap = {
for (int i = 0; i < names.length; i++) i: names[i],
};
print(nameMap); // {0: Alice, 1: Bob, 2: Carol}
// Combine collection for with collection if
var numbers = [1, 2, 3, 4, 5, 6];
var evenSquares = [
for (var n in numbers)
if (n % 2 == 0) n * n,
];
print(evenSquares); // [4, 16, 36]
// Generate Set with collection for
var uniqueDoubles = {for (var n in [1, 2, 2, 3]) n * 2};
print(uniqueDoubles); // {2, 4, 6}
}
Output:
[1, 4, 9, 16, 25]
[APPLE, BANANA, CHERRY]
{0: Alice, 1: Bob, 2: Carol}
[4, 16, 36]
{2, 4, 6}
Explanation
for (var item in iterable) iterates over each element and adds the resulting expression to the collection.
- Traditional C-style
for (int i = 0; i < n; i++) is also supported inside collection literals.
- Collection
for can be nested and combined with collection if for powerful inline data transformations.
- Works in
List, Set, and Map literals.
Notes
- Collection
for is equivalent to calling map().toList() but is often more readable for simple transformations.
- Use it to generate UI elements (e.g., Flutter widget lists) from data collections.
- Combining collection
for with collection if produces concise filter-and-transform pipelines.
Common Mistakes
- Forgetting to use
in keyword in for-in form: for (var x in list).
- Trying to add multiple elements per iteration without a spread: use
...[a, b].
- Using collection
for for very complex multi-step transformations — a regular loop is more readable in those cases.
💪 Exercise
- Use collection
for to generate a list of the first 10 multiples of 3.
- Use collection
for with collection if to build a list of all odd numbers from 1 to 20.
- Convert a list of product names to a Map where keys are the index and values are the names in title case.
🧠 Quiz
1. What does collection for produce?
- A) A single value
- B) Multiple elements added to the collection ✅
- C) A new function
- D) A boolean
2. Collection for can be combined with which other collection feature?
- A) Collection switch
- B) Collection if ✅
- C) Collection try
- D) Collection while
Summary
Collection for generates multiple elements inside a collection literal using a loop, working with both for-in and C-style for loops. It supports List, Set, and Map literals and can be combined with collection if for inline filter-and-transform operations. It is a clean alternative to map().toList() for simple data transformations.