resolveStreamForKey method

  1. @protected
void resolveStreamForKey(
  1. ImageConfiguration configuration,
  2. ImageStream stream,
  3. T key,
  4. ImageErrorListener handleError
)

Called by resolve with the key returned by obtainKey.

Subclasses should override this method rather than calling obtainKey if they need to use a key directly. The resolve method installs appropriate error handling guards so that errors will bubble up to the right places in the framework, and passes those guards along to this method via the handleError parameter.

It is safe for the implementation of this method to call handleError multiple times if multiple errors occur, or if an error is thrown both synchronously into the current part of the stack and thrown into the enclosing Zone.

The default implementation uses the key to interact with the ImageCache, calling ImageCache.putIfAbsent and notifying listeners of the stream. Implementers that do not call super are expected to correctly use the ImageCache.

Implementation

@protected
void resolveStreamForKey(ImageConfiguration configuration, ImageStream stream, T key, ImageErrorListener handleError) {
  // This is an unusual edge case where someone has told us that they found
  // the image we want before getting to this method. We should avoid calling
  // load again, but still update the image cache with LRU information.
  if (stream.completer != null) {
    final ImageStreamCompleter? completer = PaintingBinding.instance.imageCache.putIfAbsent(
      key,
      () => stream.completer!,
      onError: handleError,
    );
    assert(identical(completer, stream.completer));
    return;
  }
  final ImageStreamCompleter? completer = PaintingBinding.instance.imageCache.putIfAbsent(
    key,
    () {
      ImageStreamCompleter result = loadImage(key, PaintingBinding.instance.instantiateImageCodecWithSize);
      // This check exists as a fallback for backwards compatibility until the
      // deprecated `loadBuffer()` method is removed. Until then, ImageProvider
      // subclasses may have only overridden `loadBuffer()`, in which case the
      // base implementation of `loadWithSize()` will return a sentinel value
      // of type `_AbstractImageStreamCompleter`.
      if (result is _AbstractImageStreamCompleter) {
        result = loadBuffer(key, PaintingBinding.instance.instantiateImageCodecFromBuffer);
      }
      return result;
    },
    onError: handleError,
  );
  if (completer != null) {
    stream.setCompleter(completer);
  }
}