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
Future<ByteData> putIfAbsent(
Object key,
Future<ByteData> loader(),
) {
assert(key != null); // ignore: unnecessary_null_comparison
assert(loader != null); // ignore: unnecessary_null_comparison
Future<ByteData>? pendingResult = _pending[key];
if (pendingResult != null) {
return pendingResult;
}
ByteData? 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 {
pendingResult = loader();
_pending[key] = pendingResult;
pendingResult.then((ByteData data) {
_pending.remove(key);
_add(key, data);
result = data; // in case it was a synchronous future.
});
}
if (result != null) {
_add(key, result!);
return SynchronousFuture<ByteData>(result!);
}
assert(_cache.length <= maximumSize);
return pendingResult!;
}