get method

  1. @override
Future<V?> get(
  1. K key, {
  2. Loader<K, V>? ifAbsent,
})
override

Returns the value associated with key.

If ifAbsent is specified and no value is associated with the key, a mapping is added and the value is returned. Otherwise, returns null.

Implementation

@override
Future<V?> get(K key, {Loader<K, V>? ifAbsent}) async {
  if (_map.containsKey(key)) {
    return _map[key];
  }
  // If this key is already loading then return the existing future.
  if (_outstanding.containsKey(key)) {
    return _outstanding[key];
  }
  if (ifAbsent != null) {
    var futureOr = ifAbsent(key);
    _outstanding[key] = futureOr;
    V v;
    try {
      v = await futureOr;
    } finally {
      // Always remove key from [_outstanding] to prevent returning the
      // failed result again.
      _outstanding.remove(key);
    }
    _map[key] = v;
    return v;
  }
  return null;
}