computeIfAbsent method

V computeIfAbsent(
  1. K key,
  2. V ifAbsent(
    1. K key
    )
)

Computes a value for the given key if it is not already present in the map.

Parameters

  • key: The key to compute a value for
  • ifAbsent: The function to compute the value if the key is not present

Returns

  • The computed value

Implementation

V computeIfAbsent(K key, V Function(K key) ifAbsent) {
  if (containsKey(key)) {
    return this[key] as V;
  } else {
    final V newValue = ifAbsent(key);
    this[key] = newValue;
    return newValue;
  }
}