Chris1234567899 / flutter_time_range_picker

A time range picker for Flutter
Other
33 stars 52 forks source link

Failed assertion: line 81 pos 12: 'hour == null || (hour >= 0 && hour < hoursPerDay)': is not true #1

Closed Edijae closed 4 years ago

Edijae commented 4 years ago

I was testing my app where I'm using this library but it was throwing the failed assertion error. It was around 10:13 PM when i was doing the test. Below is the method where assertion was failing

TimeOfDay replacing({ int hour, int minute }) {
    assert(hour == null || (hour >= 0 && hour < hoursPerDay));
    assert(minute == null || (minute >= 0 && minute < minutesPerHour));
    return TimeOfDay(hour: hour ?? this.hour, minute: minute ?? this.minute);
  }

I followed where the above method was being called and i found one of it usages below.

void initState() {
    var startTime = widget.start ?? TimeOfDay.now();
    var endTime = widget.end ?? startTime.replacing(hour: startTime.hour + 3);

Something interesting above. Since it was 10 PM (22 in 24hr clock system), if you add three hours, you get 22+3 = 25. Thus the value that will be passed to startTime.replacing(hour: startTime.hour + 3) is 25 hence causing the failure.

I think the solution is after adding the 3 hours, we should check if the hour is >= 24, we return 23.

Chris1234567899 commented 4 years ago

Thanks for the issue, I fixed it.