getOrPut method

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

Gets a value or computes it if not present.

This is useful for caching expensive computations.

Implementation

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