formatFileSize property
String?
formatFileSize
Formats the String
to show its proper file size.
If the String
is not a valid integer, it is returned unchanged.
Example
String foo = '24117248';
String formatted = foo.formatFileSize; // returns '23 MB';
Implementation
String? get formatFileSize {
if (this.isBlank) {
return this;
}
var number = this.toInt();
if (number == null) {
return this;
}
List<String> suffix = ["bytes", "KB", "MB", "GB"];
int j = 0;
while (number! >= 1024 && j < 4) {
number = (number / 1024).floor();
j++;
}
return "$number ${suffix[j]}";
}