getOrPut method

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

Returns the value for the given key. If the key is not found in the map, calls the defaultValue function, puts its result into the map under the given key and returns it.

Note that the operation is not guaranteed to be atomic if the map is being modified concurrently.

Implementation

V getOrPut(K key, V Function() defaultValue) {
  final value = get(key);
  if (value != null) return value;
  final answer = defaultValue();
  put(key, answer);
  return answer;
}