A flexible, lightweight and customizable in-memory image caching service for Flutter. Optimized for decoding and caching ui.Image
instances in memory. Ideal for prefetching, smooth scroll performance, and low-latency rendering.
Supports LRU (Least Recently Used) and no-eviction strategies.
- In-memory caching of both raw image bytes (
Uint8List
) and decodedui.Image
. - Pluggable caching strategies: simple map or LRU (with byte size limit).
- Supports prefetching with deduplication and awaitable in-flight fetches.
- Customizable fetch and decode logic (e.g., via HTTP or local assets).
- Built-in default decoder using Flutter's image codec.
- Manual or automatic cache eviction.
- Stateless widget for displaying cached images (
CachedImageMemory
) with:- Fade-in animation.
- Custom loading and error widgets.
- Ready/error callbacks.
- No disk caching: built for fast runtime memory access.
dependencies:
cdx_image_cache: ^0.0.3
final cache = MemoryImageCacheService();
cache.init(
strategy: ImageCacheStrategy.lru(maxSizeBytes: 100 * 1024 * 1024), // 100MB
fetchFunction: (url) async {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) return response.bodyBytes;
throw Exception('Failed to fetch image');
},
);
You can use any HTTP client or custom logic in the fetcher.
class Article {
final String imageUrl;
Article.fromJson(Map<String, dynamic> json)
: imageUrl = json['image'] {
imageCacheService.prefetch(imageUrl);
}
}
await imageCacheService.waitForAllPrefetches()
Perfect for splash screens or onboarding screens that load bulk content.
CachedImageMemory(
url: article.imageUrl,
cacheService: imageCacheService,
fadeDuration: Duration(milliseconds: 400),
builder: (context, data) => Image.memory(data, fit: BoxFit.cover),
loadingBuilder: (context) => const CircularProgressIndicator(),
errorBuilder: (context, error) => const Icon(Icons.broken_image),
onReady: (data) => print('Image ready!'),
)
This widget is stateless and uses TweenAnimationBuilder
for fade-in.
abstract class ImageCacheStrategy {
factory ImageCacheStrategy.simple();
factory ImageCacheStrategy.lru({required int maxSizeBytes});
}
Removes least recently used items when maxEntries
is exceeded.
Grows unbounded. Use with caution.
ImageMemoryCacheService({
required CacheStrategy strategy,
required Future<Uint8List> Function(String url) fetcher,
Duration timeout = const Duration(seconds: 15),
});
Methods:
Uint8List? getIfAvailable(String url)
ui.Image? getDecodedIfAvailable(String url)
Future<ui.Image?> getDecodedImage(String url)
void prefetchImages(Iterable<String> urls)
Future<void> waitForAllFetches()
void clearCache({String? url})
MIT
Developed by Codedix