Collections
Map Methods
Dart Map exposes a few methods that make working with keys safer than raw indexing. containsKey(k) answers whether a key is present without inserting anything. putIfAbsent(k, fn) adds a key only when it is missing, building the initial value lazily. update(k, fn) rebuilds the value for an existing key by running fn(oldValue). keys and values are lazy views over the map's current entries in insertion order.
Program
Play the program to check a key, add a missing entry, update an existing entry, and read out keys and values.
map_methods.dart
Replay: real traced execution (multi-file project)
void main() {
var hits = {'home': 3, 'about': 1};
var hasHome = hits.containsKey('home');
hits.putIfAbsent('faq', () => 0);
hits.update('home', (v) => v + 1);
var keys = hits.keys.toList();
var values = hits.values.toList();
print('hasHome=$hasHome keys=$keys values=$values');
}
hits ← {home: 3, about: 1}
1void main() {2 var hits = {'home': 3, 'about': 1};3 var hasHome = hits.containsKey('home');values this step{home: 3, about: 1}hitshasHome ← true
2var hits = {'home': 3, 'about': 1};3var hasHome = hits.containsKey('home');4hits.putIfAbsent('faq', () => 0);values this steptruehasHome{home: 3, about: 1}hitshits ← {home: 3, about: 1, faq: 0}
3var hasHome = hits.containsKey('home');4hits.putIfAbsent('faq', () => 0);5hits.update('home', (v) => v + 1);values this step{home: 3, about: 1} → {home: 3, about: 1, faq: 0}hitshits ← {home: 4, about: 1, faq: 0}
4hits.putIfAbsent('faq', () => 0);5hits.update('home', (v) => v + 1);6var keys = hits.keys.toList();values this step{home: 3, about: 1, faq: 0} → {home: 4, about: 1, faq: 0}hitskeys ← [home, about, faq]
5hits.update('home', (v) => v + 1);6var keys = hits.keys.toList();7var values = hits.values.toList();values this step[home, about, faq]keysvalues ← [4, 1, 0]
6var keys = hits.keys.toList();7var values = hits.values.toList();8print('hasHome=$hasHome keys=$keys values=$values');values this step[4, 1, 0]values{home: 4, about: 1, faq: 0}hitsprint('hasHome=$hasHome keys=$keys values=$values');
7 var values = hits.values.toList();8 print('hasHome=$hasHome keys=$keys values=$values');9}outputhasHome=true keys=[home, about, faq] values=[4, 1, 0]values this steptruehasHome[home, about, faq]keys[4, 1, 0]values
Check, Add, Update
- Start with
home: 3andabout: 1. containsKey('home')returnstrue.putIfAbsent('faq', () => 0)addsfaq.update('home', (v) => v + 1)changeshomefrom3to4.- Reading keys and values shows the current map order.
| Key | Final value |
| --- | --- |
|
home|4| |about|1| |faq|0|
containsKey
`m.containsKey(k)` returns `bool` without mutating the map. Prefer it to checking `m[k] == null`, which cannot tell missing keys from null values.
putIfAbsent and update
`m.putIfAbsent(k, () => init)` inserts only when `k` is missing; the callback builds the initial value lazily. `m.update(k, (v) => newV)` rewrites an existing key's value with `newV` derived from the old.
keys and values
`m.keys` and `m.values` are lazy `Iterable` views in insertion order. Call `.toList()` to snapshot them as concrete `List`s.
Exercise: map_methods.dart
Check for a key, add a missing key, update an existing key, and print keys and values