getCacheSize static method

Future<int> getCacheSize({
  1. String? cacheSubdirectory,
})

Returns cache folder size in bytes.

Implementation

static Future<int> getCacheSize({String? cacheSubdirectory}) async {
  try {
    final cacheDirectory = await _getCacheDirectory(cacheSubdirectory, createIfMissing: false);
    if (cacheDirectory == null) {
      return 0;
    }

    int totalSize = 0;
    await for (final entity in cacheDirectory.list()) {
      if (entity is! File) {
        continue;
      }
      try {
        totalSize += await entity.length();
      } catch (e) {
        debugLog('Error getting file size ${entity.path}: $e');
      }
    }

    return totalSize;
  } catch (e) {
    debugLog('Error getting cache size: $e');
    return 0;
  }
}