パッケージ、リポジトリ、アプリカテゴリを検索

GitHub リポジトリerf/asset_cache

asset_cache

💾 Flutterで任意のローカルアセットを読み込みおよびキャッシュする

asset_cache のプロジェクト画像
スター
10
フォーク
0
最終プッシュ(UTC)
2025/06/25
プロジェクト状態
公開中

元の README

以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。

asset_cache

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.

Usage

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')

Asset Decoders

The following AssetCache implementations are included:

  • ImageAssetCache
  • JsonAssetCache
  • TextAssetCache
  • ByteDataAssetCache

Custom AssetCache

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.

Cache Management

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();
Preloading Assets

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();
Exception Handling

The package includes custom exceptions for better error handling:

  • AssetNotFoundException - Thrown when an asset cannot be found or loaded
  • DecodingException - Thrown when an asset cannot be decoded to the expected type
  • PreLoadingAssetsNotFoundException - Thrown when preloading fails for some assets
try {
  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}');
}

More

See example and test.

Erlend GitHub avatar
GitHub 作者

Erlend ↗

言語DartPythonSwiftKotlinObjective-C

カテゴリー

このリポジトリが公開するパッケージ

使用している依存関係

依存関係一覧 4 件

  • flutter{"sdk":"flutter"}
  • path^1.9.1
  • flutter_test開発用{"sdk":"flutter"}
  • flutter_lints開発用^6.0.0

プロジェクトに合うリポジトリを選ぶには?

用途で絞り込み、開発状況を比較し、採用前に元のリポジトリで要件を確認しましょう。

  1. 名前・作者・説明で検索し、技術トピックで絞り込みます。
  2. スター、フォーク、最近のプッシュを比較します。スターは関心の目安であり、品質の保証ではありません。
  3. 利用前に GitHub で README、ライセンス、Issues を確認します。

情報源と管理

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗