flutter_cache_manager 1.1.3

flutter_cache_manager #

pub package Donate

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);

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

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.

Settings #

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 http getter. 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,
      fileFetcher: _customHttpGetter);

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

  static Future<FileFetcherResponse> _customHttpGetter(String url, {Map<String, String> headers}) async {
    // Do things with headers, the url or whatever.
    return HttpFileFetcherResponse(await http.get(url, headers: headers));

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.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 #

A new Flutter project.

Getting Started #

For help getting started with Flutter, view our online documentation.

Use this package as a library

1. Depend on it

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

  flutter_cache_manager: ^1.1.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:flutter_cache_manager/flutter_cache_manager.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.5
  • Flutter: 1.12.13+hotfix.7


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
http >=0.11.0 <0.13.0 0.12.0+4
path ^1.6.2 1.6.4
path_provider >=0.5.0 <2.0.0 1.6.1
sqflite ^1.1.0 1.2.1
uuid ^2.0.0 2.0.4
Transitive dependencies
async 2.4.0
charcode 1.1.3
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.4
http_parser 3.1.3
meta 1.1.8
pedantic 1.9.0
platform 2.2.1
sky_engine 0.0.99
source_span 1.6.0
string_scanner 1.0.5
synchronized 2.2.0
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
test ^1.3.0