Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get application cache size in Flutter?

Tags:

flutter

dart

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?

like image 230
VipiN Negi Avatar asked May 26 '26 01:05

VipiN Negi


1 Answers

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;
}
like image 132
Arbaz Shaikh Avatar answered May 30 '26 07:05

Arbaz Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!