패키지, 저장소 및 앱 카테고리 검색

패키지 상세v2.2.0

asset_cache

디코더를 제공하여 어떤 유형의 바이너리 리소스도 로드하고 캐시합니다. 바이트 데이터, 이미지, JSON 및 텍스트에 대한 내장 지원이 있습니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
1.6천
좋아요
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 개

소스 저장소

이 패키지를 사용하는 프로젝트