flutter_cache_manager 1.4.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 100

flutter_cache_manager #

pub package Build Status codecov

A CacheManager to download and cache files in the cache directory of the app. Various settings on how long to keep a file can be changed.

It uses the cache-control http header to efficiently retrieve files.

The more basic usage is explained here. See the complete docs for more info.

Usage #

The cache manager can be used to get a file on various ways The easiest way to get a single file is call .getSingleFile.

    var file = await DefaultCacheManager().getSingleFile(url);

getFileStream(url) returns a stream with the first result being the cached file and later optionally the downloaded file.

getFileStream(url, withProgress: true) when you set withProgress on true, this stream will also emit DownloadProgress when the file is not found in the cache.

downloadFile(url) directly downloads from the web.

getFileFromCache only retrieves from cache and returns no file when the file is not in the cache.

putFile gives the option to put a new file into the cache without downloading it.

removeFile removes a file from the cache.

emptyCache removes all files from the cache.

Other implementations #

When your files are stored on Firebase Storage you can use flutter_cache_manager_firebase.

Customize #

The cache manager is customizable by extending the BaseCacheManager. Below is an example with other settings for the maximum age of files, maximum number of objects and a custom FileService. The key parameter in the constructor and the getFilePath method are mandatory.


class CustomCacheManager extends BaseCacheManager {
  static const key = "customCache";

  static CustomCacheManager _instance;

  factory CustomCacheManager() {
    if (_instance == null) {
      _instance = new CustomCacheManager._();
    }
    return _instance;
  }

  CustomCacheManager._() : super(key,
      maxAgeCacheObject: Duration(days: 7),
      maxNrOfCacheObjects: 20);

  Future<String> getFilePath() async {
    var directory = await getTemporaryDirectory();
    return p.join(directory.path, key);
  }
}

How it works #

By default the cached files are stored in the temporary directory of the app. This means the OS can delete the files any time.

Information about the files is stored in a database using sqflite. The file name of the database is the key of the cacheManager, that's why that has to be unique.

This cache information contains the end date till when the file is valid and the eTag to use with the http cache-control.

[1.4.0] - 2020-06-04 #

  • Allow cleaning of memory cache (PR #183).
  • Bugfix: Cleaning doesn't want to delete a file twice anymore (PR #185).

[1.3.0] - 2020-05-28 #

  • Basic web support. (At least it downloads the file for you.)
  • Support for the following mimetypes:
    • application/vnd.android.package-archive (apk)
    • audio/x-aac (aac)
    • video/quicktime (mov)

[1.2.2] - 2020-04-16 #

  • Support for RxDart 0.24.x

[1.2.1] - 2020-04-14 #

  • Fixed optional parameters in the Content-Type header (#164).

[1.2.0] - 2020-04-10 #

  • Added getFileStream to CacheManager
    • getFileStream has an optional parameter 'withProgress' to receive progress.
    • getFileStream returns a FileResponse which is either a FileInfo or a DownloadProgress.
  • Changes to FileFetcher and FileFetcherResponse:
    • FileFetcher is now replaced with a FileService which is a class instead of a function.
    • FileServiceResponse doesn't just give magic headers, but concrete implementation of the needed information.
    • FileServiceResponse gives a contentStream instead of content for more efficient handling of the data.
    • FileServiceResponse contains contentLength with information about the total size of the content.
  • Changes in CacheStore for testability:
    • CleanupRunMinInterval can now be set.
    • Expects a mockable directory instead of a path.
  • Added CacheInfoRepository interface to possibly replace the current CacheObjectProvider based on sqflite.
  • Changes in WebHelper
    • Files are now always saved with a new name. Files are first saved to storage before old file is removed.
  • General code quality improvements

[1.1.3] - 2019-10-17 #

  • Use try-catch in WebHelper so VM understands that errors are not uncaught.

[1.1.2] - 2019-10-16 #

  • Better error handling (really better this time).
  • Fix that oldest files are removed, and not the newest.
  • Fix error when cache data exists, but file is already removed.
  • await on putFile

[1.1.1] - 2019-07-23 #

  • Changed error handling back to throwing the error as it is supposed to be.

[1.1.0] - 2019-07-13 #

  • New method to get fileinfo from memory.
  • Better error handling.

[1.0.0] - 2019-06-27 #

  • Keep SQL connection open during session.
  • Update dependencies

[0.3.2] - 2019-03-06 #

  • Fixed image loading after loading failed once.

[0.3.1] - 2019-02-27 #

  • Added method to clear cache

[0.3.0] - 2019-02-18 #

  • Complete refactor of library
  • Use of SQFlite instead of shared preferences for cache info
  • Added the option to use a custom file fetcher (for example for firebase)
  • Support for AndroidX

[0.2.0] - 2018-10-13 #

  • Fixed library compatibility issue

[0.1.2] - 2018-08-30 #

  • Fixed library compatibility issue
  • Improved some synchronization

[0.1.1] - 2018-04-27 #

  • Fixed some issues when file could not be downloaded the first time it is trying to be retrieved.

[0.1.0] - 2018-04-14 #

  • Fixed ConcurrentModificationError in cache cleaning
  • Added optional headers
  • Moved to Dart 2.0

[0.0.4+1] - 2018-02-16 #

  • Fixed nullpointer when non-updated file (a 304 response) has no cache-control period.

[0.0.4] - 2018-01-31 #

  • Fixed issues with cache cleaning

[0.0.3] - 2018-01-08 #

  • Fixed relative paths on iOS.

[0.0.2] - 2017-12-29 #

  • Did some refactoring and made a useful readme.

[0.0.1] - 2017-12-28 #

  • Extracted the cache manager from cached_network_image

example/lib/main.dart

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Cache Manager Demo',
      home: MyHomePage(),
    );
  }
}

const url = 'https://blurha.sh/assets/images/img1.jpg';

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

class _MyHomePageState extends State<MyHomePage> {
  Stream<FileResponse> fileStream;

  void _downloadFile() {
    setState(() {
      fileStream = DefaultCacheManager().getFileStream(url, withProgress: true);
    });
  }

  @override
  Widget build(BuildContext context) {
    if (fileStream == null) {
      return Scaffold(
        appBar: _appBar(),
        body: const ListTile(
            title: Text('Tap the floating action button to download.')),
        floatingActionButton: Fab(
          downloadFile: _downloadFile,
        ),
      );
    }
    return DownloadPage(
      fileStream: fileStream,
      downloadFile: _downloadFile,
      clearCache: _clearCache,
    );
  }

  void _clearCache() {
    DefaultCacheManager().emptyCache();
    setState(() {
      fileStream = null;
    });
  }
}

class DownloadPage extends StatelessWidget {
  final Stream<FileResponse> fileStream;
  final VoidCallback downloadFile;
  final VoidCallback clearCache;
  const DownloadPage(
      {Key key, this.fileStream, this.downloadFile, this.clearCache})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<FileResponse>(
      stream: fileStream,
      builder: (context, snapshot) {
        Widget body;

        var loading = !snapshot.hasData || snapshot.data is DownloadProgress;

        if (snapshot.hasError) {
          body = ListTile(
            title: const Text('Error'),
            subtitle: Text(snapshot.error.toString()),
          );
        } else if (loading) {
          body = ProgressIndicator(progress: snapshot.data as DownloadProgress);
        } else {
          body = FileInfoWidget(
            fileInfo: snapshot.data as FileInfo,
            clearCache: clearCache,
          );
        }

        return Scaffold(
          appBar: _appBar(),
          body: body,
          floatingActionButton: !loading
              ? Fab(
                  downloadFile: downloadFile,
                )
              : null,
        );
      },
    );
  }
}

AppBar _appBar() {
  return AppBar(
    title: const Text('Flutter Cache Manager Demo'),
  );
}

class Fab extends StatelessWidget {
  final VoidCallback downloadFile;
  const Fab({Key key, this.downloadFile}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: downloadFile,
      tooltip: 'Download',
      child: Icon(Icons.cloud_download),
    );
  }
}

class ProgressIndicator extends StatelessWidget {
  final DownloadProgress progress;
  const ProgressIndicator({Key key, this.progress}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            width: 50.0,
            height: 50.0,
            child: CircularProgressIndicator(
              value: progress?.progress,
            ),
          ),
          const SizedBox(width: 20.0),
          const Text('Downloading'),
        ],
      ),
    );
  }
}

class FileInfoWidget extends StatelessWidget {
  final FileInfo fileInfo;
  final VoidCallback clearCache;

  const FileInfoWidget({Key key, this.fileInfo, this.clearCache})
      : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        ListTile(
          title: const Text('Original URL'),
          subtitle: Text(fileInfo.originalUrl),
        ),
        if (fileInfo.file != null)
          ListTile(
            title: const Text('Local file path'),
            subtitle: Text(fileInfo.file.path),
          ),
        ListTile(
          title: const Text('Loaded from'),
          subtitle: Text(fileInfo.source.toString()),
        ),
        ListTile(
          title: const Text('Valid Until'),
          subtitle: Text(fileInfo.validTill.toIso8601String()),
        ),
        Padding(
          padding: const EdgeInsets.all(10.0),
          child: RaisedButton(
            child: const Text('CLEAR CACHE'),
            onPressed: clearCache,
          ),
        ),
      ],
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_cache_manager: ^1.4.0

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:flutter_cache_manager/flutter_cache_manager.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
99
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]
100
Learn more about scoring.

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

  • Dart: 2.8.2
  • pana: 0.13.8-dev
  • Flutter: 1.17.1

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
clock ^1.0.1 1.0.1
file ^5.1.0 5.1.0
flutter 0.0.0
http ^0.12.0+2 0.12.1
path ^1.6.4 1.7.0
path_provider ^1.4.0 1.6.10
pedantic ^1.8.0+1 1.9.0
rxdart >=0.23.1 <0.25.0 0.24.1
sqflite ^1.1.7+2 1.3.0+2 1.3.1-dev.1
uuid ^2.0.2 2.0.4
Transitive dependencies
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.5
http_parser 3.1.4
intl 0.16.1
meta 1.1.8
path_provider_linux 0.0.1+1
path_provider_macos 0.0.4+3
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
sky_engine 0.0.99
source_span 1.7.0
sqflite_common 1.0.1
string_scanner 1.0.5
synchronized 2.2.0
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
xdg_directories 0.1.0
Dev dependencies
flutter_test
mockito ^4.1.1