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

パッケージ詳細v2.2.0

asset_cache

デコーダーを指定して、任意の種類のバイナリーアセットを読み込み、キャッシュします。バイトデータ、画像、JSON、テキストのサポートが内蔵されています。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
869
いいね数
42
Pub ポイント
160

パッケージをインストール

flutter pub add asset_cache

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

AssetCache は、カスタマイズ可能なデコーダーを使用して、任意の種類のバイナリアセットを効率的に読み込み、キャッシュするためのFlutter/Dartパッケージです。バイトデータ、画像、JSON、テキストなどの一般的なアセットタイプに対して組み込みサポートを提供し、重複するネットワークまたはファイルI/O操作を減らします。最初の読み込み後にアセットをキャッシュすることで、アプリのパフォーマンスを向上させ、レイテンシを低減します。このパッケージはプラットフォームに依存せず、Android、iOS、Web、macOS、Windows、Linuxをサポートしており、ローカルアセット管理に依存するクロスプラットフォームアプリに最適です。

用途

  • アセットからの画像をキャッシュする
  • パースされたJSONデータをローカルに保存する
  • テキストファイルを効率的に読み込む
  • 繰り返しのアセット読み込みを減らす
  • アプリの起動時間を改善する
  • オフラインモードでバイナリデータを管理する

主な機能

  • カスタマイズ可能なデコーダー
  • 複数のアセットタイプに対する組み込みサポート
  • クロスプラットフォーム互換性
  • 効率的なキャッシュメカニズム
  • 資産取得のためのシンプルなAPI
  • オフライン資産アクセスをサポート

適している対象

  • 頻繁にアセットを読み込むアプリ
  • オフライン優先のアプリケーション
  • メディア重視のアプリ
  • パフォーマンスが重要なアプリ
  • 柔軟なアセット処理を必要とする開発者
  • 一貫したアセットキャッシュが必要なプロジェクト

注意事項

  • 適切なデコーダーの実装が必要です
  • メモリ使用量は、キャッシュされたアセットのサイズに依存します
  • 大規模な動的データには適していません
  • 長期間実行されるアプリケーションでは注意深く管理が必要です
  • 静的アセットソースに制限
  • 複雑なデコードロジックのための追加のセットアップが必要になる場合があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

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.

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト