fileSize static method

String fileSize(
  1. num bytes, {
  2. int precision = 0,
  3. int? maxPrecision,
})

Formats bytes as a human-readable file size.

Implementation

static String fileSize(num bytes, {int precision = 0, int? maxPrecision}) {
  const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  var i = 0;
  var value = bytes.toDouble();
  while (value.abs() >= 1024 && i < units.length - 1) {
    value /= 1024;
    i++;
  }
  return '${_formatNumber(value, precision, maxPrecision)} ${units[i]}';
}