singleValueCache<T> function

T Function() singleValueCache<T>(
  1. T compute()
)

Returns a function that computes once and returns cached value.

A null result is cached too: a computed flag (not ??=) gates the call, so a null-returning compute still runs only once.

Implementation

T Function() singleValueCache<T>(T Function() compute) {
  bool computed = false;
  late T cached;
  return () {
    if (!computed) {
      cached = compute();
      computed = true;
    }
    return cached;
  };
}