getOrPut method

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

Returns the value for key. If absent, computes it with compute, stores it, and returns it.

final cache = <String, int>{};
cache.getOrPut('key', () => expensiveCompute()); // computes once

Implementation

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