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.
var cacheManager = await CacheManager.getInstance(); var file = await cacheManager.getFile(url);
Some settings of the CacheManager can be changed. All these preferences are statics and should be set before the first use of the CacheManager, so preferably directly on start of your app.
For extra logging set:
CacheManager.showDebugLogs = true;
The cache can be cleaned after it is used to get a file. By default this happens once every week. You can change this by setting
CacheManager.inBetweenCleans = new Duration(days: 7);
The CacheManager checks for two things, for objects that are too old and the size of the cache.
By default it removes objects that haven't been used for 30 days. Set this by
maxAgeCacheObject. *This is not about when the object is first downloaded, but when it is used the last.
CacheManager.maxAgeCacheObject = new Duration(days: 30);
By default the cache size is set to 200, when the cache grows beyond this it will remove the oldest objects again by when last used. Set this with
CacheManager.maxNrOfCacheObjects = 200;
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 the shared preferences with the key "lib_cached_image_data". (Because images was the first use of this library :)) The date when the cache is last cleaned is stored as "lib_cached_image_data_last_clean".
This cache information contains the end date till when the file is valid and the eTag to use with the http cache-control.
Add this to your package's pubspec.yaml file:
dependencies: flutter_cache_manager: "^0.1.1"
You can install packages from the command line:
$ flutter packages get
Alternatively, your editor might support
flutter packages get.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
|0.1.1||Apr 27, 2018|
|0.1.0||Apr 14, 2018|
|0.0.4+1||Feb 16, 2018|
|0.0.4||Jan 31, 2018|
|0.0.3||Jan 8, 2018|
|0.0.2||Dec 29, 2017|
|0.0.1||Dec 28, 2017|
|0.1.0-rc.1||Mar 24, 2018|
We analyzed this package on Jun 12, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
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]
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
The description is too short.
Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.
Package is pre-v1 release.
While there is nothing inherently wrong with versions of
0.*.*, it usually means that the author is still experimenting with the general direction API.
Fix analysis and formatting issues.
Analysis or formatting checks reported 8 hints.
Strong-mode analysis of
lib/flutter_cache_manager.dartgave the following hint:
line: 28 col: 13
'synchronized' is deprecated and shouldn't be used.
Strong-mode analysis of
lib/src/cache_object.dartgave the following hint:
line: 95 col: 48
'onError' is deprecated and shouldn't be used.
Maintain an example.
Create a short demo in the
example/directory to show how to use this package. Common file name patterns include:
example.dartor you could also use
|Dart SDK||>=2.0.0-dev.28.0 <3.0.0|