flutterdarttimeofday

Flutter - Convert minutes to TimeOfDay


Currently I get a number from the BE that states like 750 minutes (int) and should represent the time of day.

750 minutes = 12.5 hours and so the UI should display 12:30

1080 minutes = 18 hours and so the UI should display 18:00

But I can't seem to find a way to get a clean convertion from minutes to a proper Object with hours and minutes.

In the end I want to have a TimeOfDay object

I will continue to struggle and will also post if I find the answer myself :)


Solution

  • With the help of the comments above I resolved it by splitting the sting. I don't see this as the best way to convert since I'm dependened that the Duration Class will never change but never the less I got what I wanted by doing the following

      TimeOfDay minutesToTimeOfDay(int minutes) {
          Duration duration = Duration(minutes: minutes);
          List<String> parts = duration.toString().split(':');
          return TimeOfDay(hour: int.parse(parts[0]), minute: int.parse(parts[1]));
      }
    

    Create Duration with the amount of minutes, then toString() that instance this will outpout 00:00 and then split this string on the colon ":"