parseHumanReadableSize function
Parse a human-readable file size (e.g., KB, MB, GB) to bytes.
// '10': 10, '30B': 30, '1.0K': 1024, '2.0KB': 2048, '4.0KiB': 4096, '1.5 K': 1536, '1024 KB': 1048576, '2.0M': 2097152, '3.1G': 3328599654, '4.2T': 4617948836659, '5.3P': 5967269506265907,
print(parseHumanReadableSize('1K')); // 1024
print(parseHumanReadableSize('2.0KB')); // 2048
print(parseHumanReadableSize('2.0M')); // 2097152
Implementation
int? parseHumanReadableSize(String humanSize) {
final splitSize = splitHumanReadableSize(humanSize);
if (splitSize == null) return null; // Invalid format
final (value, unit) = splitSize;
return switch (unit) {
'B' => value.toInt(),
'K' => (value * sizeKB).toInt(),
'M' => (value * sizeMB).toInt(),
'G' => (value * sizeGB).toInt(),
'T' => (value * sizeTB).toInt(),
'P' => (value * sizePB).toInt(),
_ => null // Invalid suffix
};
}