generateHumanReadableFilesize static method

String generateHumanReadableFilesize(
  1. num size, {
  2. Bases base = Bases.Binary,
  3. int decimals = 3,
})

Generates a human readable string in xiB or xB from an int size in bytes Provide base (binary/metric) and decimals Defaults to binary units and 3 decimals

Implementation

static String generateHumanReadableFilesize(num size,
    {Bases base = Bases.Binary, int decimals = 3}) {
  if (size > (basesValue[base] ?? 1024)) {
    int index = ((_logBase(size, basesValue[base] ?? 1024)).floor());
    String unit = units[index];

    num value = size / (Math.pow(basesValue[base] ?? 1024, index) * 1.0);

    return "${value.toStringAsFixed(decimals)} $unit${basesString[base]}";
  }
  return "${size.toStringAsFixed(decimals)} B";
}