My application is image-based and I'm using CachedNetworkImage to handle images from network. I'd like to show users cached images size on the device and an option to clean within the app. I am able to clean the app's cache using flutter_cache_manager.
To clean app cache:
await DefaultCacheManager().emptyCache();
There is no such function to get the app's cache size. How do I do get it?
You can use dart:io package to get the cache size of your application:
import 'dart:io';
Future<int> getCacheSize() async {
Directory tempDir = await getTemporaryDirectory();
int tempDirSize = _getSize(tempDir);
return tempDirSize;
}
int _getSize(FileSystemEntity file) {
if (file is File) {
return file.lengthSync();
} else if (file is Directory) {
int sum = 0;
List<FileSystemEntity> children = file.listSync();
for (FileSystemEntity child in children) {
sum += _getSize(child);
}
return sum;
}
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With