formatBytes function

String formatBytes(
  1. int bytes,
  2. int decimals
)

Implementation

String formatBytes(int bytes, int decimals) {
  if (bytes <= 0) return '0 B';
  const List<String> units = ['B', 'KB', 'MB', 'GB', 'TB'];
  int digitGroups = (math.log(bytes) / math.log(1024)).floor();
  return '${(bytes / math.pow(1024, digitGroups)).toStringAsFixed(decimals)} ${units[digitGroups]}';
}