fileSizeWithUnit function

String fileSizeWithUnit(
  1. int bytes
)

Converts byte length to human readable format.

Implementation

String fileSizeWithUnit(int bytes) {
  if (bytes <= 0) return '0 B';
  const List<String> suffixes = <String>['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  final int i = (log(bytes) / log(1024)).floor();
  return '${(bytes / pow(1024, i)).toStringAsFixed(0)} ${suffixes[i]}';
}