resolveStreamForKey method

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

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

@override
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 = imageCache.putIfAbsent(
      key,
      () => stream.completer!,
      onError: handleError,
    );
    assert(identical(completer, stream.completer));
    return;
  }
  final ImageStreamCompleter? completer = imageCache.putIfAbsent(
    key,
    () => loadImage(
      key,
      PaintingBinding.instance.instantiateImageCodecWithSize,
    ),
    onError: handleError,
  );
  if (completer != null) {
    stream.setCompleter(completer);
  }
}