搜尋套件、儲存庫與 APP 分類

套件詳情v2.2.0

asset_cache

給定解碼器,載入並快取任意類型的二進制資源。內建支援位元資料、影像、json 與文字。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1550
喜歡數
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 項

原始碼儲存庫

使用此套件的專案