Skip to content

Instantly share code, notes, and snippets.

@UsamaKarim
Last active December 20, 2022 11:41
Show Gist options
  • Save UsamaKarim/9877909c2ca06f9f2e34822c4cb6a664 to your computer and use it in GitHub Desktop.
Save UsamaKarim/9877909c2ca06f9f2e34822c4cb6a664 to your computer and use it in GitHub Desktop.
Cache Image read from memory bytes [Uint8List], Usually it is used while using base64 decoded image for faster result.
// Improved version from https://gist.github.com/darmawan01/9be266df44594ea59f07032e325ffa3b
class CacheMemoryImageProvider extends ImageProvider<CacheMemoryImageProvider> {
final String tag; //the cache id use to get cache
final Uint8List img; //the bytes of image to cache
CacheMemoryImageProvider(this.tag, this.img);
@override
ImageStreamCompleter loadBuffer(
CacheMemoryImageProvider key, DecoderBufferCallback decode) {
return MultiFrameImageStreamCompleter(
codec: _loadAsync(decode),
scale: 1.0,
debugLabel: tag,
informationCollector: () sync* {
yield ErrorDescription('Tag: $tag');
},
);
}
Future<Codec> _loadAsync(DecoderBufferCallback decode) async {
// the DefaultCacheManager() encapsulation, it get cache from local storage.
final Uint8List bytes = img;
if (bytes.lengthInBytes == 0) {
// The file may become available later.
PaintingBinding.instance.imageCache.evict(this);
throw StateError('$tag is empty and cannot be loaded as an image.');
}
final buffer = await ImmutableBuffer.fromUint8List(bytes);
return await decode(buffer);
}
@override
Future<CacheMemoryImageProvider> obtainKey(ImageConfiguration configuration) {
return SynchronousFuture<CacheMemoryImageProvider>(this);
}
@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType) return false;
bool res = other is CacheMemoryImageProvider && other.tag == tag;
return res;
}
@override
int get hashCode => tag.hashCode;
@override
String toString() =>
'${objectRuntimeType(this, 'CacheImageProvider')}("$tag")';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment