@@ -35,33 +35,32 @@ class CacheManager implements BaseCacheManager {
3535 /// The [fileService] can be used to customize how files are downloaded. For example
3636 /// to edit the urls, add headers or use a proxy. You can also choose to supply
3737 /// a CacheStore or WebHelper directly if you want more customization.
38- CacheManager (Config config) {
39- _config = config;
40- _store = CacheStore (config);
38+ CacheManager (Config config)
39+ : _config = config,
40+ _store = CacheStore (config) {
4141 _webHelper = WebHelper (_store, config.fileService);
4242 }
4343
4444 @visibleForTesting
4545 CacheManager .custom (
4646 Config config, {
47- CacheStore cacheStore,
48- WebHelper webHelper,
49- }) {
50- _config = config;
51- _store = cacheStore ?? CacheStore (config);
47+ CacheStore ? cacheStore,
48+ WebHelper ? webHelper,
49+ }) : _config = config,
50+ _store = cacheStore ?? CacheStore (config) {
5251 _webHelper = webHelper ?? WebHelper (_store, config.fileService);
5352 }
5453
55- Config _config;
54+ final Config _config;
5655
5756 /// Store helper for cached files
58- CacheStore _store;
57+ final CacheStore _store;
5958
6059 /// Get the underlying store helper
6160 CacheStore get store => _store;
6261
6362 /// WebHelper to download and store files
64- WebHelper _webHelper;
63+ late final WebHelper _webHelper;
6564
6665 /// Get the underlying web helper
6766 WebHelper get webHelper => _webHelper;
@@ -74,8 +73,8 @@ class CacheManager implements BaseCacheManager {
7473 @override
7574 Future <File > getSingleFile (
7675 String url, {
77- String key,
78- Map <String , String > headers,
76+ String ? key,
77+ Map <String , String >? headers,
7978 }) async {
8079 key ?? = url;
8180 final cacheFile = await getFileFromCache (key);
@@ -95,12 +94,12 @@ class CacheManager implements BaseCacheManager {
9594 @override
9695 @Deprecated ('Prefer to use the new getFileStream method' )
9796 Stream <FileInfo > getFile (String url,
98- {String key, Map <String , String > headers}) {
97+ {String ? key, Map <String , String >? headers}) {
9998 return getFileStream (
10099 url,
101100 key: key,
102101 withProgress: false ,
103- ).map ((r) => r as FileInfo );
102+ ).where ((r) => r is FileInfo ). cast < FileInfo >( );
104103 }
105104
106105 /// Get the file from the cache and/or online, depending on availability and age.
@@ -116,18 +115,17 @@ class CacheManager implements BaseCacheManager {
116115 /// might be outdated and a new file is being downloaded in the background.
117116 @override
118117 Stream <FileResponse > getFileStream (String url,
119- {String key, Map <String , String > headers, bool withProgress}) {
118+ {String ? key, Map <String , String >? headers, bool withProgress = false }) {
120119 key ?? = url;
121120 final streamController = StreamController <FileResponse >();
122- _pushFileToStream (
123- streamController, url, key, headers, withProgress ?? false );
121+ _pushFileToStream (streamController, url, key, headers, withProgress);
124122 return streamController.stream;
125123 }
126124
127125 Future <void > _pushFileToStream (StreamController streamController, String url,
128- String key, Map <String , String > headers, bool withProgress) async {
126+ String ? key, Map <String , String >? headers, bool withProgress) async {
129127 key ?? = url;
130- FileInfo cacheFile;
128+ FileInfo ? cacheFile;
131129 try {
132130 cacheFile = await getFileFromCache (key);
133131 if (cacheFile != null ) {
@@ -166,7 +164,9 @@ class CacheManager implements BaseCacheManager {
166164 ///Download the file and add to cache
167165 @override
168166 Future <FileInfo > downloadFile (String url,
169- {String key, Map <String , String > authHeaders, bool force = false }) async {
167+ {String ? key,
168+ Map <String , String >? authHeaders,
169+ bool force = false }) async {
170170 key ?? = url;
171171 var fileResponse = await _webHelper
172172 .downloadFile (
@@ -182,13 +182,13 @@ class CacheManager implements BaseCacheManager {
182182 /// Get the file from the cache.
183183 /// Specify [ignoreMemCache] to force a re-read from the database
184184 @override
185- Future <FileInfo > getFileFromCache (String key,
185+ Future <FileInfo ? > getFileFromCache (String key,
186186 {bool ignoreMemCache = false }) =>
187187 _store.getFile (key, ignoreMemCache: ignoreMemCache);
188188
189189 ///Returns the file from memory if it has already been fetched
190190 @override
191- Future <FileInfo > getFileFromMemory (String key) =>
191+ Future <FileInfo ? > getFileFromMemory (String key) =>
192192 _store.getFileFromMemory (key);
193193
194194 /// Put a file in the cache. It is recommended to specify the [eTag] and the
@@ -201,15 +201,19 @@ class CacheManager implements BaseCacheManager {
201201 Future <File > putFile (
202202 String url,
203203 Uint8List fileBytes, {
204- String key,
205- String eTag,
204+ String ? key,
205+ String ? eTag,
206206 Duration maxAge = const Duration (days: 30 ),
207207 String fileExtension = 'file' ,
208208 }) async {
209209 key ?? = url;
210210 var cacheObject = await _store.retrieveCacheData (key);
211- cacheObject ?? = CacheObject (url,
212- key: key, relativePath: '${Uuid ().v1 ()}.$fileExtension ' );
211+ cacheObject ?? = CacheObject (
212+ url,
213+ key: key,
214+ relativePath: '${const Uuid ().v1 ()}.$fileExtension ' ,
215+ validTill: DateTime .now ().add (maxAge),
216+ );
213217
214218 cacheObject = cacheObject.copyWith (
215219 validTill: DateTime .now ().add (maxAge),
@@ -233,17 +237,18 @@ class CacheManager implements BaseCacheManager {
233237 Future <File > putFileStream (
234238 String url,
235239 Stream <List <int >> source, {
236- String key,
237- String eTag,
240+ String ? key,
241+ String ? eTag,
238242 Duration maxAge = const Duration (days: 30 ),
239243 String fileExtension = 'file' ,
240244 }) async {
241245 key ?? = url;
242246 var cacheObject = await _store.retrieveCacheData (key);
243247 cacheObject ?? = CacheObject (url,
244248 key: key,
245- relativePath: '${Uuid ().v1 ()}'
246- '.$fileExtension ' );
249+ relativePath: '${const Uuid ().v1 ()}'
250+ '.$fileExtension ' ,
251+ validTill: DateTime .now ().add (maxAge));
247252
248253 cacheObject = cacheObject.copyWith (
249254 validTill: DateTime .now ().add (maxAge),
@@ -267,8 +272,8 @@ class CacheManager implements BaseCacheManager {
267272 @override
268273 Future <void > removeFile (String key) async {
269274 final cacheObject = await _store.retrieveCacheData (key);
270- if (cacheObject != null ) {
271- await _store.removeCachedFile (cacheObject);
275+ if (cacheObject? .id != null ) {
276+ await _store.removeCachedFile (cacheObject! );
272277 }
273278 }
274279
0 commit comments