formatCardValue function

String formatCardValue(
  1. num? value,
  2. String unit
)

Display value for a card tile: bytes as KB/MB, ms as ms (or s above 1000), else as-is.

Implementation

String formatCardValue(num? value, String unit) {
  if (value == null) return 'n/a';
  final v = value.toDouble();
  if (unit == 'B') {
    if (v.abs() >= 1e6) return '${(v / 1e6).toStringAsFixed(1)} MB';
    if (v.abs() >= 1e3) return '${(v / 1e3).round()} KB';
    return '${v.round()} B';
  }
  if (unit == 'ms') {
    if (v >= 1000) return '${(v / 1000).toStringAsFixed(1)} s';
    return '${v.round()} ms';
  }
  return '${v.round()}';
}