loadImage method

Converts a key into an ImageStreamCompleter, and begins fetching the image.

For backwards-compatibility the default implementation of this method returns an object that will cause resolveStreamForKey to consult loadBuffer. However, implementors of this interface should only override this method and not loadBuffer, which is deprecated.

The decode callback provides the logic to obtain the codec for the image.

See also:

  • ResizeImage, for modifying the key to account for cache dimensions.

Implementation

@override
ImageStreamCompleter loadImage(
  CachedNetworkImageProvider key,
  ImageDecoderCallback decode,
) {
  final chunkEvents = StreamController<ImageChunkEvent>();
  final imageStreamCompleter = MultiImageStreamCompleter(
    codec: _loadImageAsync(key, chunkEvents, decode),
    chunkEvents: chunkEvents.stream,
    scale: key.scale,
    informationCollector: () sync* {
      yield DiagnosticsProperty<ImageProvider>(
        'Image provider: $this \n Image key: $key',
        this,
        style: DiagnosticsTreeStyle.errorProperty,
      );
    },
  );

  if (errorListener != null) {
    imageStreamCompleter.addListener(
      ImageStreamListener(
        (image, synchronousCall) {},
        onError: (Object error, StackTrace? trace) {
          errorListener?.call(error);
        },
      ),
    );
  }

  return imageStreamCompleter;
}