Search packages, repositories and app categories

Package detailsv2.2.0

asset_cache

Load and cache binary asset of any type given a decoder. Built in support for byte data, images, json and text.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
869
Likes
42
Pub points
160

Install package

flutter pub add asset_cache

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

AssetCache is a Flutter/Dart package designed to efficiently load and cache binary assets of any type using a customizable decoder. It provides built-in support for common asset types such as byte data, images, JSON, and text, reducing redundant network or file I/O operations. By caching assets after first load, it improves app performance and reduces latency. The package is platform-agnostic, supporting Android, iOS, Web, macOS, Windows, and Linux, making it ideal for cross-platform applications that rely on local asset management.

Use cases

  • Caching images from assets
  • Storing parsed JSON data locally
  • Loading text files efficiently
  • Reducing repeated asset loading
  • Improving app startup time
  • Managing binary data in offline mode

Key features

  • Customizable decoders
  • Built-in support for multiple asset types
  • Cross-platform compatibility
  • Efficient caching mechanism
  • Simple API for asset retrieval
  • Supports offline asset access

Suitable for

  • Apps with frequent asset loading
  • Offline-first applications
  • Media-heavy apps
  • Performance-critical apps
  • Developers needing flexible asset handling
  • Projects requiring consistent asset caching

Considerations

  • Requires proper decoder implementation
  • Memory usage depends on cached asset size
  • Not intended for large dynamic data
  • Needs careful management in long-running apps
  • Limited to static asset sources
  • May require additional setup for complex decoding logic

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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.

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package