0Pricing
Dart Academy · Lesson

Iterating Entries, Keys, and Values

Loop a map and use putIfAbsent.

Iterating Entries, Keys, and Values is a free Dart Academy lesson on CoddyKit — lesson 4 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.

Looping a Map

Sometimes you need to visit every pair in a Map. Dart gives you several clean ways to walk through it. 🚶

Just the Keys

The keys property gives an iterable of every key. Loop it when you only care about the labels.

var m = {'a': 1, 'b': 2};
for (var k in m.keys) {
  print(k);
}

Just the Values

The values property gives an iterable of every value, ignoring the keys entirely.

var m = {'a': 1, 'b': 2};
for (var v in m.values) {
  print(v);
}

Pairs With entries

The entries property gives MapEntry objects so you can reach both the key and its value at once.

for (var e in m.entries) {
  print(e.key);
  print(e.value);
}

The forEach Shortcut

The forEach method hands you each key and value directly, making per-pair work read very cleanly.

m.forEach((key, value) {
  print('$key: $value');
});

Meet putIfAbsent

Use putIfAbsent to set a key only when it is missing. Existing keys are left untouched.

var m = {'a': 1};
m.putIfAbsent('b', () => 2);

Why putIfAbsent Helps

The value function in putIfAbsent runs only when needed, so you avoid wasteful work for keys already present.

m.putIfAbsent('a', () => expensive());

Updating a Value

The update method changes an existing key with a function, perfect for counters and tallies.

var counts = {'a': 1};
counts.update('a', (v) => v + 1);

update With a Fallback

Pass ifAbsent to update so a brand-new key still gets a starting value instead of an error.

counts.update('z', (v) => v + 1,
  ifAbsent: () => 1);

Reshaping With map

The map method transforms every entry into a new pair, returning a fresh map without touching the original.

var doubled = m.map((k, v) =>
  MapEntry(k, v * 2));

Order Is Insertion Order

A default Map remembers the order you inserted keys, so loops visit pairs in that same reliable order.

Quick Check

Which property gives you both the key and value together in one loop?

Recap

Nicely done! Loop with keys, values, or entries, and reach for putIfAbsent and update to manage pairs smartly. 🎉

Frequently asked questions

Is the “Iterating Entries, Keys, and Values” lesson free?

Yes — the full text of “Iterating Entries, Keys, and Values” 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 “Iterating Entries, Keys, and Values”?

Loop a map and use putIfAbsent. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Iterating Entries, Keys, and Values” 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. Sets and Guaranteed Uniqueness
  2. Set Operations: union, intersection, difference
  3. Maps as Key-Value Stores
  4. Iterating Entries, Keys, and Values
← Back to Dart Academy