toString method Null safety

String toString ()
override

Returns a string representation of this Duration.

Returns a string with hours, minutes, seconds, and microseconds, in the following format: H:MM:SS.mmmmmm. For example,

var d = Duration(days: 1, hours: 1, minutes: 33, microseconds: 500);
d.toString();  // "25:33:00.000500"

d = Duration(days: 0, hours: 1, minutes: 10, microseconds: 500);
d.toString();  // "1:10:00.000500"

Implementation

String toString() {
  String sixDigits(int n) {
    if (n >= 100000) return "$n";
    if (n >= 10000) return "0$n";
    if (n >= 1000) return "00$n";
    if (n >= 100) return "000$n";
    if (n >= 10) return "0000$n";
    return "00000$n";
  }

  String twoDigits(int n) {
    if (n >= 10) return "$n";
    return "0$n";
  }

  if (inMicroseconds < 0) {
    return "-${-this}";
  }
  String twoDigitMinutes =
      twoDigits(inMinutes.remainder(minutesPerHour) as int);
  String twoDigitSeconds =
      twoDigits(inSeconds.remainder(secondsPerMinute) as int);
  String sixDigitUs =
      sixDigits(inMicroseconds.remainder(microsecondsPerSecond) as int);
  return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs";
}