asset_cache
デコーダーを指定して、任意の種類のバイナリーアセットを読み込み、キャッシュします。バイトデータ、画像、JSON、テキストのサポートが内蔵されています。
💾 Flutterで任意のローカルアセットを読み込みおよびキャッシュする
{"sdk":"flutter"}^1.9.1{"sdk":"flutter"}^6.0.0以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。
asset_cache will load and cache any asset of type T given a decoder.
It was made as CachingAssetBundle does not cache binary data.
Binary resources (from load) are not cached.
Describe assets in your pubspec.yaml:
assets:
- assets/images/
- assets/json/
Create asset cache instances with an optional basePath to avoid full paths:
final imageCache = ImageAssetCache(basePath: "assets/images/");
final jsonCache = JsonAssetCache(basePath: "assets/json/");
Load and cache assets using load returns a Future<T>:
final image = await imageCache.load('sprite.png')
final json = await jsonCache.load('sprite.json')
The following AssetCache implementations are included:
Create a custom asset cache by inheriting from AssetCache and pass it a AssetDecoder.
You can also inherit from GenericCache and override loadAsset, if you don't want to load from a AssetBundle.
The cache provides several management methods:
// Load and cache an asset (returns cached version if already loaded)
final image = await imageCache.load('sprite.png');
// Check if an asset is cached
if (imageCache.contains('sprite.png')) {
// Asset is already cached
}
// Get the number of cached items
print('Cached items: ${imageCache.size}');
// Remove a specific cached item
imageCache.remove('sprite.png');
// Clear all cached items
imageCache.clear();
For better performance, you can preload multiple assets concurrently:
// Preload multiple assets at once
await imageCache.preload(['sprite1.png', 'sprite2.png', 'sprite3.png']);
// Remember to call this before preloading at app startup
WidgetsFlutterBinding.ensureInitialized();
The package includes custom exceptions for better error handling:
AssetNotFoundException - Thrown when an asset cannot be found or loadedDecodingException - Thrown when an asset cannot be decoded to the expected typePreLoadingAssetsNotFoundException - Thrown when preloading fails for some assetstry {
final image = await imageCache.load('missing.png');
} on AssetNotFoundException catch (e) {
print('Asset not found: ${e.assetKey}');
} on DecodingException catch (e) {
print('Failed to decode asset: ${e.assetKey}');
}