loadAsync method
Stream<Codec>
loadAsync(
- String url,
- String? cacheKey,
- StreamController<
ImageChunkEvent> chunkEvents, - dynamic decode,
- BaseCacheManager cacheManager,
- int? maxHeight,
- int? maxWidth,
- Map<
String, String> ? headers, - dynamic errorListener()?,
- ImageRenderMethodForWeb imageRenderMethodForWeb,
- 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();
}
}