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.
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.
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));
}
}
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.
example/README.md
A new Flutter project.
For help getting started with Flutter, view our online documentation.
Add this to your package's pubspec.yaml file:
dependencies:
flutter_cache_manager: ^0.3.0
You can install packages from the command line:
with Flutter:
$ 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:
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.3.0 | Feb 18, 2019 |
|
|
0.2.0+1 | Nov 12, 2018 |
|
|
0.2.0 | Oct 13, 2018 |
|
|
0.1.2 | Aug 30, 2018 |
|
|
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 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
96
|
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]
|
98
|
We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Format lib/src/web_helper.dart
.
Run flutter format
to format lib/src/web_helper.dart
.
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+1 | |
path | ^1.6.2 | 1.6.2 | |
path_provider | ^0.5.0+1 | 0.5.0+1 | |
sqflite | ^1.1.0 | 1.1.0+1 | |
synchronized | ^2.0.2 | 2.0.2+1 | |
uuid | ^2.0.0 | 2.0.0 | |
Transitive dependencies | |||
async | 2.0.8 | ||
charcode | 1.1.2 | ||
collection | 1.14.11 | ||
convert | 2.1.1 | ||
crypto | 2.0.6 | ||
http_parser | 3.1.3 | ||
meta | 1.1.6 | 1.1.7 | |
sky_engine | 0.0.99 | ||
source_span | 1.5.4 | ||
string_scanner | 1.0.4 | ||
term_glyph | 1.1.0 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 | ||
Dev dependencies | |||
flutter_test | |||
test | ^1.3.0 |