getDirectorySize method

Future<String> getDirectorySize(
  1. String dirPath
)

Get human-readable directory size

Implementation

Future<String> getDirectorySize(String dirPath) async {
  final dir = Directory(dirPath);
  if (!dir.existsSync()) {
    return 'Unknown';
  }

  var totalBytes = 0;
  await for (final entity in dir.list(recursive: true)) {
    if (entity is File) {
      try {
        totalBytes += await entity.length();
      } catch (e) {
        // Skip files that can't be read
        continue;
      }
    }
  }

  return _formatBytes(totalBytes);
}