'How to save flutter TimeOfDay to firebase?

I am working on a flutter application where I am showing a TimeRangepicker using this plugin: time_range_picker and I am getting TimeOfDay(09:00) + TimeOfDay(12:00) as a result by using this code:

  onPressed: () async {
                        TimeRange result = await showTimeRangePicker(
                            use24HourFormat: false,
                            interval: Duration(minutes: 30),
                            context: context,
                            start: TimeOfDay(hour: 9, minute: 0),
                            end: TimeOfDay(hour: 12, minute: 0),
                            disabledTime: TimeRange(
                                startTime: TimeOfDay(hour: 23, minute: 0),
                                endTime: TimeOfDay(hour: 5, minute: 0)),
                            disabledColor: Colors.red.withOpacity(0.5),
                            strokeWidth: 4,
                            ticks: 24,
                            ticksOffset: -7,
                            ticksLength: 15,
                            ticksColor: Colors.grey,
                            labels: [
                              "12 pm",
                              "3 am",
                              "6 am",
                              "9 am",
                              "12 am",
                              "3 pm",
                              "6 pm",
                              "9 pm"
                            ].asMap().entries.map((e) {
                              return ClockLabel.fromIndex(
                                  idx: e.key, length: 8, text: e.value);
                            }).toList(),
                            labelOffset: 35,
                            rotateLabels: false,
                            padding: 60);

                        print("${result.startTime} + ${result.endTime}");
                      },

But the only problem is I can find an appropriate way to save this to firebase, I don't need date all I need is TimeOfDay.



Solution 1:[1]

In the common use case whereby only the hour and minute are required a simple map of these values will suffice.

Map timeOfDayToFirebase(TimeOfDay timeOfDay){
    return {
        'hour':timeOfDay.hour,
        'minute':timeOfDay.minute
           }
}


TimeOfDay firebaseToTimeOfDay(Map data){
        return TimeOfDay(
            hour: data['hour'],
            minute: data['minute']);
}



var myTimeOfDayObject=TimeOfDay.now();
firebase.update({'time': timeOfDayToFirebase(myTimeOfDayObject)});

This way you don't have to worry about managing offsets, unintended locale changes and so forth.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 matwr