image_cache_sync 0.0.1
image_cache_sync: ^0.0.1 copied to clipboard
A lightweight Flutter package for caching network images locally with offline support and smart sync using update timestamps.
image_cache_sync #
A lightweight Flutter package for caching network images locally with offline support and smart synchronization using update timestamps.
This package is designed to efficiently download, store, and reuse images while keeping them in sync with backend updates.
๐ Features #
- ๐ฅ Download and cache images locally
- ๐พ Persistent storage using Hive
- ๐ Smart sync using
updatedAttimestamps - ๐ฆ Bulk image handling (pagination friendly)
- ๐ด Offline support (works without internet after first load)
- ๐งน Cache management (clear/delete images)
- ๐จ Easy-to-use Flutter widget (
CachedImageView) - โก Efficient network usage (downloads only when needed)
๐ง Why use this package? #
In many apps, images are:
- Loaded repeatedly from network โ
- Not available offline โ
- Not synced with backend updates โ
This package solves that by:
- Caching images locally
- Reusing cached images
- Updating only when required
๐ฆ Installation #
Add this to your pubspec.yaml:
dependencies:
image_cache_sync: ^0.0.1
Then run:
flutter pub get
๐งช Usage #
๐น 1. Display Image (Recommended) #
CachedImageView(
id: "1",
url: "https://your-api.com/image.jpg",
updatedAt: DateTime(2024, 1, 1),
)
๐น 2. Fetch Image Manually #
final manager = ImageCacheManager();
final image = await manager.fetchImage(
id: "1",
url: "https://your-api.com/image.jpg",
updatedAt: DateTime(2024, 1, 1),
);
print(image.localPath);
๐น 3. Bulk Sync (Pagination Friendly) #
final manager = ImageCacheManager();
final images = await manager.syncImages([
ImageItem(
id: "1",
url: "https://your-api.com/image.jpg",
localPath: "",
updatedAt: DateTime(2024, 1, 1),
),
]);
๐น 4. Clear Cache #
final manager = ImageCacheManager();
await manager.clearAll();
๐น 5. Get Cached Image Only #
final cached = await manager.getCached("1");
๐ง How It Works #
Step 1: Check Cache #
The package checks if the image already exists locally.
Step 2: Compare Timestamp #
It compares updatedAt from API with cached value.
Step 3: Decide Action #
| Condition | Action |
|---|---|
| Not cached | Download image |
Cached + same updatedAt |
Use local image |
| Cached + updatedAt changed | Re-download |
Step 4: Store Data #
- Image file โ local storage
- Metadata โ Hive database
๐ฑ Pagination Support #
This package works seamlessly with paginated APIs.
- Sync happens per item (not per page)
- Only current page needs syncing
- Efficient for large datasets
๐ด Offline Support #
Once images are cached:
- No internet required
- Images load instantly
- Works even in airplane mode
๐งน Cache Management #
Clear all cached data #
await manager.clearAll();
This will:
- Remove all images from storage
- Clear Hive database
๐ฏ Use Cases #
- ๐ E-commerce apps
- ๐ฐ News apps
- ๐ธ Gallery apps
- ๐ฑ Social media feeds
- ๐ก Offline-first apps
โ ๏ธ Important Notes #
updatedAtmust come from your API- Changing
updatedAttriggers re-download - Ensure
idis unique for each image - URL is required for sync functionality
๐ Example Flow #
- App loads image โ downloads & caches
- App restarts โ loads from cache
- Backend updates image โ
updatedAtchanges - Package detects change โ re-downloads
๐ฎ Future Improvements (Planned) #
- Placeholder & error widgets
- Retry mechanism
- Cache size control
- Performance optimizations
๐ค Contributing #
Contributions are welcome! Feel free to open issues or submit pull requests.
๐ License #
MIT License