← → or space · progress saves for Continue on the roadmap
Goal
Read, transform, filter, and summarize collection data.
Step 1 - Simple iteration with for-in
void main() {
List<int> numbers = [2, 4, 6];
for (final n in numbers) {
print(n);
}
}- Use
for-inwhen you just need to read each item.
Step 2 - map (transform each item)
void main() {
List<int> numbers = [1, 2, 3];
List<int> doubled = numbers.map((n) => n * 2).toList();
print(doubled); // [2, 4, 6]
}mapreturns transformed values.
Step 3 - where (filter items)
void main() {
List<int> numbers = [1, 2, 3, 4, 5, 6];
List<int> evens = numbers.where((n) => n.isEven).toList();
print(evens); // [2, 4, 6]
}wherekeeps only items that match the condition.
Step 4 - fold (combine into one result)
void main() {
List<int> prices = [100, 250, 50];
int total = prices.fold(0, (sum, item) => sum + item);
print(total); // 400
}foldis useful for totals, counts, and summaries.
Practice tasks
- Convert a
List<String>to uppercase usingmap. - Keep only words longer than 3 letters using
where. - Sum a list of expenses using
fold.