getOrFetch method

Future<V> getOrFetch(
  1. K key,
  2. Future<V> provider()
)

Gets or computes the value for the provided key using the provided provider function and store it in the cache using the provided key.

If provider is set in the constructor this method will throw

Implementation

Future<V> getOrFetch(K key, Future<V> Function() provider) {
  if (this.provider != null) {
    throw ArgumentError(
      'getOrFetch can only be used if a provider is not in the constructor',
    );
  }
  return _cache
      .putIfAbsent(
        key,
        () => SingleValueCache(maxAge: maxAge),
      )
      .getOrFetch(provider);
}