formatBytes function

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

Format bytes for download

Implementation

String formatBytes(int bytes, int decimals) {
  if (bytes <= 0) return "0Mb";
  const k = 1024;
  final dm = decimals < 0 ? 0 : decimals;
  final sizes = [' Bytes', ' KB', ' MB', ' GB'];
  int i = (log(bytes) / log(k)).floor();
  return '${(bytes / pow(k, i)).toStringAsFixed(dm)}${sizes[i]}';
}