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)),
                            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() {
                              return ClockLabel.fromIndex(
                                  idx: e.key, length: 8, text: e.value);
                            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.


There is no specific data type in Firestore for storing a time of day, so you’ll have to map it to one of the existing types.

Some common mapping are:

  • Store the time of day as a string value, such as "07:55:00.000" (if you care up to millisecond precision)
  • Store the time of day as an offset from midnight, such as 28500 (as the number of seconds from midnight until 7:55 AM)
  • Store the time of day as a the time part of a fixed day in a Timestamp field. This is essentially a variant of the previous approach, with the offset being from the start of the epoch. Say that you pick the start of the epoch (January 1, 1970) as the fixed day, then the Timestamp’s value would be 28500000 (in milliseconds).

All of these have valid use-case, so pick whichever one works best for the use-cases of your app.

Answered By – Frank van Puffelen

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *