asset_cache 0.2.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 75

asset_cache #

asset_cache will load and cache any asset of type T given a decoder.

Since CachingAssetBundle does not cache binary data, we load and cache data ourselves.

Binary resources (from load) are not cached.

Create a custom asset cache by inheriting from AssetCache and pass it a decoder. Or inherit from GenericCache and override loadAsset.

We include the following singletons for loading various asset types:

  • ImageAssets
  • JsonAssets
  • StringAssets
  • ByteDataAssets

Usage #

Add asset_cache to your pubspec.yaml:

dependencies:
  asset_cache:

Describe assets in your pubspec.yaml:

  assets:
     - assets/images/
     - assets/json/

Set AssetCache.basePath to avoid using full asset paths:

ImageAssets.instance.basePath = "assets/images/";

Load and cache assets using load, which returns a Future<T>:

final image = await ImageAssets.instance.load('sprite.png')

See example and test

Based on CachingAssetBundle.

[0.2.3] #

  • update README

[0.2.2] #

  • update README

[0.2.1] #

  • update pubspec.yaml
  • format

[0.2.0] #

  • split up asset_cache in multiple files
  • changed Uint8List decoder class to use ByteData
  • updated README

[0.1.4] #

  • update README | format

[0.1.3] #

  • update README

[0.1.2] #

  • update README

[0.1.1] #

  • update README

[0.1.0] #

  • initial release
  • generic asset cache for loading assets given a decoder
  • a set of asset cache implementations for images, json files and strings
  • a generic cache class
  • tests
  • examples

example/lib/main.dart

import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:asset_cache/asset_cache.dart';
import 'package:flutter/material.dart';

void main() {
  StringAssets.instance.basePath = 'assets/strings/';
  ByteDataAssets.instance.basePath = 'assets/images/';
  JsonAssets.instance.basePath = 'assets/json/';
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'asset cache demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('asset cache demo'),
      ),
      body: Center(
        child: Column(
          children: [
            FutureBuilder(
              future: StringAssets.instance.load('hello.txt'),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Text(snapshot.data);
                } else {
                  return Text('loading..');
                }
              },
            ),
            FutureBuilder<ByteData>(
              future: ByteDataAssets.instance.load('angel.png'),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Image.memory(snapshot.data.buffer.asUint8List());
                } else {
                  return Text('loading..');
                }
              },
            ),
            FutureBuilder(
              future: JsonAssets.instance.load('sprite.json'),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Text(snapshot.data.toString());
                } else {
                  return Text('loading..');
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  asset_cache: ^0.2.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:asset_cache/asset_cache.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
50
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [asset_cache] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test