toDuration method

Duration toDuration()

Assumes a string (roughly) of the format '\d{1,2}:\d{2}'

Implementation

Duration toDuration() {
  final chunks = split(':');
  if (chunks.length == 1) {
    throw Exception('Invalid duration string: $this');
  } else if (chunks.length == 2) {
    return Duration(
      minutes: int.parse(chunks[0].trim()),
      seconds: int.parse(chunks[1].trim()),
    );
  } else if (chunks.length == 3) {
    return Duration(
      hours: int.parse(chunks[0].trim()),
      minutes: int.parse(chunks[1].trim()),
      seconds: int.parse(chunks[2].trim()),
    );
  } else {
    throw Exception('Invalid duration string: $this');
  }
}