formattedSize property

String get formattedSize

Returns a human-readable formatted file size string (e.g. "1.4 MB", "340 KB").

Implementation

String get formattedSize {
  final numBytes = size ?? bytes?.lengthInBytes;
  if (numBytes == null || numBytes <= 0) return '0 B';
  const suffixes = ['B', 'KB', 'MB', 'GB', 'TB'];
  final i = (log(numBytes) / log(1024)).floor();
  final clampedI = i.clamp(0, suffixes.length - 1);
  final value = numBytes / pow(1024, clampedI);
  return '${value.toStringAsFixed(value < 10 && clampedI > 0 ? 1 : 0)} ${suffixes[clampedI]}';
}