putIfAbsent method

Returns the previously cached PictureStream for the given key, if available; if not, calls the given callback to obtain it first. In either case, the key is moved to the "most recently used" position.

The arguments must not be null. The loader cannot return null.

Implementation

PictureStreamCompleter putIfAbsent(
  Object key,
  PictureStreamCompleter loader(),
) {
  assert(key != null); // ignore: unnecessary_null_comparison
  assert(loader != null); // ignore: unnecessary_null_comparison
  PictureStreamCompleter? result = _cache[key];
  if (result != null) {
    // Remove the provider from the list so that we can put it back in below
    // and thus move it to the end of the list.
    _cache.remove(key);
  } else {
    if (_cache.length == maximumSize && maximumSize > 0) {
      _cache.remove(_cache.keys.first)!.cached = false;
    }
    result = loader();
  }
  if (maximumSize > 0) {
    assert(_cache.length < maximumSize);
    _cache[key] = result;
    result.cached = true;
  }
  assert(_cache.length <= maximumSize);
  return result;
}