call method

T call()

Gets or computes the lazy-initialized value.

On the first call, executes the initialization function, caches the result, and returns it. On subsequent calls, returns the cached value without re-executing the initialization function.

Example:

final config = Once(() => loadConfig());

final settings1 = config(); // Loads config
final settings2 = config(); // Returns cached config

Implementation

T call() {
  if (!_computed) {
    _value = _init();
    _computed = true;
  }

  return _value ??= _init();
}