getOrPut method

V getOrPut(
  1. K key,
  2. V defaultValue()
)

Returns the value for the key or computes it if not found.

The computed value is stored in the map.

Example:

final map = <String, int>{};
map.getOrPut('count', () => 0); // 0 (and stores it)
map.getOrPut('count', () => 0); // 0 (retrieves existing)

Implementation

V getOrPut(K key, V Function() defaultValue) {
  if (!containsKey(key)) {
    this[key] = defaultValue();
  }
  return this[key]!;
}