fromJson method

  1. @override
TimeOfDay? fromJson(
  1. String? json
)

Implementation

@override
TimeOfDay? fromJson(String? json) {
  if (json == null) {
    return null;
  }

  String _removeLeadingZeroIfNeeded(String value) {
    if (value.startsWith('0')) {
      const indexOfSecondCharacter = 1;
      return value.substring(indexOfSecondCharacter);
    } else {
      return value;
    }
  }

  final elements = json.split(':');
  final hourString = _removeLeadingZeroIfNeeded(elements.first);
  final minuteString = _removeLeadingZeroIfNeeded(elements.last);

  final hour = int.tryParse(hourString);
  final minute = int.tryParse(minuteString);

  if (hour != null && minute != null) {
    return TimeOfDay(hour: hour, minute: minute);
  } else {
    return null;
  }
}