{"id":21260720,"url":"https://github.com/erf/asset_cache","last_synced_at":"2025-07-11T03:31:35.036Z","repository":{"id":56826245,"uuid":"260343584","full_name":"erf/asset_cache","owner":"erf","description":"💾 load and cache any local asset in Flutter","archived":false,"fork":false,"pushed_at":"2025-06-25T17:37:35.000Z","size":203,"stargazers_count":9,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-02T15:52:46.300Z","etag":null,"topics":["cache","cache-storage","dart","flutter","memory-cache"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/asset_cache","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erf.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-30T23:56:21.000Z","updated_at":"2025-06-25T17:37:39.000Z","dependencies_parsed_at":"2022-09-20T22:54:44.669Z","dependency_job_id":null,"html_url":"https://github.com/erf/asset_cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/erf/asset_cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erf%2Fasset_cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erf%2Fasset_cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erf%2Fasset_cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erf%2Fasset_cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erf","download_url":"https://codeload.github.com/erf/asset_cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erf%2Fasset_cache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264721305,"owners_count":23653917,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","cache-storage","dart","flutter","memory-cache"],"created_at":"2024-11-21T04:20:19.884Z","updated_at":"2025-07-11T03:31:35.030Z","avatar_url":"https://github.com/erf.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# asset_cache\n\n**asset_cache** will load and cache any asset of type `T` given a decoder. \n\nIt was made as [CachingAssetBundle](https://api.flutter.dev/flutter/services/CachingAssetBundle-class.html) does not cache binary data.\n\n\u003e Binary resources (from load) are not cached.\n\n## Usage\n\nDescribe assets in your `pubspec.yaml`:\n\n```\n  assets:\n     - assets/images/\n     - assets/json/\n```\n\nCreate asset cache instances with an optional `basePath` to avoid full paths:\n\n```\nfinal imageCache = ImageAssetCache(basePath: \"assets/images/\");\nfinal jsonCache = JsonAssetCache(basePath: \"assets/json/\");\n```\n\nLoad and cache assets using `load` returns a `Future\u003cT\u003e`:\n\n```\nfinal image = await imageCache.load('sprite.png')\nfinal json = await jsonCache.load('sprite.json')\n```\n\n## Asset Decoders\n\nThe following `AssetCache` implementations are included:\n\n- ImageAssetCache\n- JsonAssetCache\n- TextAssetCache\n- ByteDataAssetCache\n\n## Custom AssetCache\n\nCreate a custom asset cache by inheriting from `AssetCache` and pass it a `AssetDecoder`.\n\nYou can also inherit from `GenericCache` and override `loadAsset`, if you don't want to load from a `AssetBundle`.\n\n### Cache Management\n\nThe cache provides several management methods:\n\n```dart\n// Load and cache an asset (returns cached version if already loaded)\nfinal image = await imageCache.load('sprite.png');\n\n// Check if an asset is cached\nif (imageCache.contains('sprite.png')) {\n  // Asset is already cached\n}\n\n// Get the number of cached items\nprint('Cached items: ${imageCache.size}');\n\n// Remove a specific cached item\nimageCache.remove('sprite.png');\n\n// Clear all cached items\nimageCache.clear();\n```\n\n### Preloading Assets\n\nFor better performance, you can preload multiple assets concurrently:\n\n```dart\n// Preload multiple assets at once\nawait imageCache.preload(['sprite1.png', 'sprite2.png', 'sprite3.png']);\n\n// Remember to call this before preloading at app startup\nWidgetsFlutterBinding.ensureInitialized();\n```\n\n### Exception Handling\n\nThe package includes custom exceptions for better error handling:\n\n- `AssetNotFoundException` - Thrown when an asset cannot be found or loaded\n- `DecodingException` - Thrown when an asset cannot be decoded to the expected type\n- `PreLoadingAssetsNotFoundException` - Thrown when preloading fails for some assets\n\n```dart\ntry {\n  final image = await imageCache.load('missing.png');\n} on AssetNotFoundException catch (e) {\n  print('Asset not found: ${e.assetKey}');\n} on DecodingException catch (e) {\n  print('Failed to decode asset: ${e.assetKey}');\n}\n```\n\n## More\n\nSee [example](example) and [test](test).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferf%2Fasset_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferf%2Fasset_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferf%2Fasset_cache/lists"}