[Flutter] DartでByteを単位接頭辞を付けた形式にフォーマット

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

参考(https://gist.github.com/zzpmaster/ec51afdbbfa5b2bf6ced13374ff891d9)