From da34103dacaddc76a31c16a3887c04081edf6954 Mon Sep 17 00:00:00 2001 From: perlatus Date: Wed, 9 May 2018 22:39:11 -0400 Subject: [PATCH] Add getFileIfCached method to CacheManager --- lib/flutter_cache_manager.dart | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/flutter_cache_manager.dart b/lib/flutter_cache_manager.dart index 551d9943..30745621 100644 --- a/lib/flutter_cache_manager.dart +++ b/lib/flutter_cache_manager.dart @@ -183,6 +183,26 @@ class CacheManager { } } + /// Get the file from the cache if previously loaded from [url]. If the file + /// is not cached, Future returns null. + Future getFileIfCached(String url) async { + var cacheObject = _cacheData[url]; + if (cacheObject == null) { + return null; + } + + return await synchronized(cacheObject.lock, () async { + cacheObject.touch(); + var filePath = await cacheObject.getFilePath(); + if (filePath == null) { + return null; + } + + File cachedFile = new File(filePath); + return cachedFile.existsSync() ? cachedFile : null; + }); + } + ///Get the file from the cache or online. Depending on availability and age Future getFile(String url, {Map headers}) async { String log = "[Flutter Cache Manager] Loading $url";