how to clear cache in CachedNetworkImage flutter
I agree with MichaelM, Don't use CachedNetworkImage.If you show image like this :
Image.network(
_headImageUrl,
fit: BoxFit.fitHeight,
)
you can use those code to clean image cache:
PaintingBinding.instance.imageCache.clear();
Firstly add the package (flutter_cache_manager) to pubspec.yaml file as following:
dependencies: flutter: sdk: flutter flutter_cache_manager: ^1.1.3
After a day, I found the solution. Use the DefaultCacheManager object by calling emptyCache() method, this clears the cache data.
DefaultCacheManager manager = new DefaultCacheManager();
manager.emptyCache(); //clears all data in cache.
Since CachedNetworkImage version 2.3, all these solutions won't work because it cached images in 2 different places (DefaultCacheManager & NetworkImageProvider)
So the only solution is using the evictFromCache
built-in method from CachedNetworkImage
like this:
Future _deleteImageFromCache() async {
String url = "your url";
await CachedNetworkImage.evictFromCache(url);
}
evictFromCache
is a Static method so it doesn't require to have the CachedNetworkImage
in your Widget tree, you can use it directly from any place.
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
await DefaultCacheManager().removeFile('YOUR_URL');
DONT USE emptyCache() from the first answer, it clear ALL your Cache