Flutter中如何获取应用程序缓存大小?

2024-02-15

我的应用程序是基于图像的,我正在使用缓存网络图像 https://pub.dev/packages/cached_network_image处理来自网络的图像。我想向用户展示设备上缓存的图像大小以及在应用程序中清理的选项。我可以使用清理应用程序的缓存flutter_cache_manager https://pub.dev/packages/flutter_cache_manager.

清理应用程序缓存:

await DefaultCacheManager().emptyCache();

没有这样的函数来获取应用程序的缓存大小。我怎样才能得到它?


您可以使用 dart:io 包来获取应用程序的缓存大小:

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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter中如何获取应用程序缓存大小? 的相关文章

随机推荐