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
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');
}
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.