formatBytes static method

String formatBytes(
  1. int bytes, [
  2. int decimals = 2
])

Formats byte size into human readable format (e.g. 1.5 MB).

Implementation

static String formatBytes(int bytes, [int decimals = 2]) {
  if (bytes <= 0) return "0 B";
  const suffixes = ["B", "KB", "MB", "GB", "TB"];
  var i = (math.log(bytes) / math.log(1024)).floor();
  return '${(bytes / math.pow(1024, i)).toStringAsFixed(decimals)} ${suffixes[i]}';
}