loadAsync method

  1. @override
Stream<Codec> loadAsync(
  1. String url,
  2. String? cacheKey,
  3. StreamController<ImageChunkEvent> chunkEvents,
  4. dynamic decode,
  5. BaseCacheManager cacheManager,
  6. int? maxHeight,
  7. int? maxWidth,
  8. Map<String, String>? headers,
  9. dynamic errorListener()?,
  10. ImageRenderMethodForWeb imageRenderMethodForWeb,
  11. dynamic evictImage(),
)
override

Implementation

@override
Stream<ui.Codec> loadAsync(
  String url,
  String? cacheKey,
  StreamController<ImageChunkEvent> chunkEvents,
  DecoderCallback decode,
  BaseCacheManager cacheManager,
  int? maxHeight,
  int? maxWidth,
  Map<String, String>? headers,
  Function()? errorListener,
  ImageRenderMethodForWeb imageRenderMethodForWeb,
  Function() evictImage,
) async* {
  try {
    assert(
        cacheManager is ImageCacheManager ||
            (maxWidth == null && maxHeight == null),
        'To resize the image with a CacheManager the '
        'CacheManager needs to be an ImageCacheManager. maxWidth and '
        'maxHeight will be ignored when a normal CacheManager is used.');

    var stream = cacheManager is ImageCacheManager
        ? cacheManager.getImageFile(url,
            maxHeight: maxHeight,
            maxWidth: maxWidth,
            withProgress: true,
            headers: headers,
            key: cacheKey)
        : cacheManager.getFileStream(url,
            withProgress: true, headers: headers, key: cacheKey);

    await for (var result in stream) {
      if (result is DownloadProgress) {
        chunkEvents.add(ImageChunkEvent(
          cumulativeBytesLoaded: result.downloaded,
          expectedTotalBytes: result.totalSize,
        ));
      }
      if (result is FileInfo) {
        var file = result.file;
        var bytes = await file.readAsBytes();
        var decoded = await decode(bytes.sublist(1));
        yield decoded;
      }
    }
  } catch (e) {
    // Depending on where the exception was thrown, the image cache may not
    // have had a chance to track the key in the cache at all.
    // Schedule a microtask to give the cache a chance to add the key.
    scheduleMicrotask(() {
      evictImage();
    });

    errorListener?.call();
    rethrow;
  } finally {
    await chunkEvents.close();
  }
}