formatBytes static method
Formats bytes to a human-readable string with appropriate units
@param bytes The number of bytes to format @param decimals The number of decimal places (default: 2) @return The formatted string with appropriate unit (bytes, KB, MB, GB, TB)
Implementation
static String formatBytes(int bytes, [int decimals = 2]) {
if (bytes <= 0) return "0 B";
const suffixes = ["bytes", "KB", "MB", "GB", "TB"];
int i = (bytes == 0) ? 0 : (log(bytes) / log(1024)).floor();
double size = bytes / (1 << (10 * i));
return "${NumberFormat('0.${''.padLeft(decimals, '#')}').format(size)} ${suffixes[i]}";
}