0Pricing
Dart Academy · Lesson

for-in Over Collections

Walk every item without an index.

for-in Over Collections is a free Dart Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Skip the Index

Often you just want each item, not its position. The for-in loop hands you items one at a time with no counter to manage. ✨

The for-in Shape

Write for, a variable name, the keyword in, then your collection. Each pass binds the variable to the next element.

for (var item in items) {
  print(item);
}

Looping a List

Point for-in at a List and it visits every element in order, front to back, without you touching length or brackets.

var fruits = ['apple', 'pear'];
for (var f in fruits) {
  print(f);
}

Cleaner Than Indexing

Compared with a classic for loop, for-in removes the counter, the length check, and the bracket lookup. Less code means fewer bugs.

for (var f in fruits) {
  print(f);
}

Typed Loop Variable

You can name the type instead of var for clarity. Here each element is explicitly a String as the loop walks the list.

for (String name in names) {
  print(name);
}

Looping a Set

A Set works with for-in too. You get each unique value once, though the order is not guaranteed like a list.

var ids = {1, 2, 3};
for (var id in ids) {
  print(id);
}

Any Iterable

for-in works on any Iterable, not just lists and sets. That includes ranges, map keys, and the results of map or where.

for (var k in scores.keys) {
  print(k);
}

Looping Map Entries

To loop a Map, walk its entries. Each entry exposes a key and a value you can read together in one pass.

for (var e in scores.entries) {
  print('${e.key}: ${e.value}');
}

Read-Only by Habit

Treat the loop variable as a copy of each value. Reassigning it does not change the original collection element.

for (var n in nums) {
  n = n * 2; // does not change nums
}

Do Not Modify While Looping

Adding or removing items from a collection during a for-in throws an error. Build a new list instead, or loop a copy.

for (var x in list) {
  // list.add(x); // throws!
}

forEach Alternative

The forEach method does the same job with a callback. for-in is usually clearer and supports break and continue.

fruits.forEach((f) => print(f));

Quick Check

You want to print every value in a List without managing an index. What is the cleanest choice?

Recap

You learned the for-in loop: visit each item of any Iterable with no counter. Loop lists, sets, and map entries cleanly. 🎯

Frequently asked questions

Is the “for-in Over Collections” lesson free?

Yes — the full text of “for-in Over Collections” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “for-in Over Collections”?

Walk every item without an index. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “for-in Over Collections” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Classic for Loop
  2. while and do-while Loops
  3. for-in Over Collections
  4. break, continue, and Labels
← Back to Dart Academy