image_cache_uint8list 0.0.1
image_cache_uint8list: ^0.0.1 copied to clipboard
Download image from URL, cache it, and return Uint8List.
image_cache_uint8list #
A Flutter package to download images from a URL, cache them locally, and return the image data as Uint8List.
This package is useful when you need full control over image bytes, such as using Image.memory, uploading cached images, or performing image processing.
โจ Features #
- Download images from a URL
- Cache images locally using
flutter_cache_manager - Return image data as
Uint8List - Support custom HTTP headers (e.g. Authorization)
- Customizable cache TTL (time-to-live) per request
- Automatic cache reuse on subsequent requests
๐ฆ Installation #
Add this to your pubspec.yaml:
dependencies:
image_cache_uint8list: ^0.0.1
Then run:
flutter pub get
๐ธ Demo #
[Demo]
This example demonstrates:
- Image loading as
Uint8List - Cache hit vs network fetch
- Response time measurement (ms)
๐ Usage #
Default usage (TTL = 1 day) #
final bytes = await ImageCacheUint8List.getImageBytes(
'https://example.com/image.jpg',
);
Image.memory(bytes);
If no TTL is provided, the image will be cached for 1 day by default.
Custom TTL (cache expiration) #
You can control how long an image stays in cache by specifying a custom ttl:
final bytes = await ImageCacheUint8List.getImageBytes(
'https://example.com/image.jpg',
ttl: const Duration(hours: 2),
);
Image.memory(bytes);
Each unique TTL value uses its own internal cache manager to ensure accurate expiration behavior.
Using Authorization headers #
This is useful when loading images from protected or private APIs:
final bytes = await ImageCacheUint8List.getImageBytes(
'https://secure.api.com/image.png',
ttl: const Duration(minutes: 30),
headers: {
'Authorization': 'Bearer token',
},
);
Image.memory(bytes);
๐ง How It Works #
- The package first checks the local cache using the image URL as the cache key.
- If the cached file exists and has not expired (within the specified TTL), it returns the cached image as Uint8List.
- If the cache is missing or expired, the image is downloaded from the network, stored in cache, and then returned.
- Different TTL values are managed using separate internal CacheManager instances.
๐งน Cache Management #
Clear all cached images
await ImageCacheUint8List.clearAllCaches();
This removes all cached images across all TTL configurations.
โ ๏ธ Notes & Best Practices #
- Using many different TTL values may increase disk usage, as each TTL creates a separate cache space.
- For most applications, it is recommended to use a small set of TTL values (e.g. 30 minutes, 1 hour, 1 day).
- This package focuses on returning raw image bytes and does not replace widgets like Image.network.
๐งช Testing #
This package includes unit tests with mocked HTTP responses to ensure reliable and deterministic cache behavior.
flutter test