Error with date time picker package flutter when pressing cancel on the picker

Viewed 29

Hi I am using the datetime_picker_formfield class and if i press cancel and try to reenter the prompt it does not let me, cancelling without a value also errors:

Error: Unexpected null value.
    at Object.throw_ [as throw] (http://localhost:50965/dart_sdk.js:5080:11)
    at Object.nullCheck (http://localhost:50965/dart_sdk.js:5399:30)
    at alerts.AddEventPopup.new.<anonymous> (http://localhost:50965/packages/fbla_lettering_point_app/Pages/Admin/settings.dart.lib.js:8642:115)
    at Generator.next (<anonymous>)
    at http://localhost:50965/dart_sdk.js:40641:33
    at _RootZone.runUnary (http://localhost:50965/dart_sdk.js:40511:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:50965/dart_sdk.js:35438:29)
    at handleValueCallback (http://localhost:50965/dart_sdk.js:35999:49)
    at _Future._propagateToListeners (http://localhost:50965/dart_sdk.js:36037:17)
    at [_completeWithValue] (http://localhost:50965/dart_sdk.js:35872:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:50965/dart_sdk.js:35906:35)
    at Object._microtaskLoop (http://localhost:50965/dart_sdk.js:40778:13)
    at _startMicrotaskLoop (http://localhost:50965/dart_sdk.js:40784:13)
    at http://localhost:50965/dart_sdk.js:36261:9

Here is the code:

Padding(
                        padding: const EdgeInsets.all(8),
                        child: DateTimeField(
                          decoration: const InputDecoration(
                            fillColor: mainColor,
                            border: OutlineInputBorder(),
                            labelText: "Date and Time",
                          ),
                          format: format,
                          controller: _dateController,
                          onShowPicker: (context, currentValue) async {
                            ref.read(_dateDataProvider.state).state =
                                (await showDatePicker(
                                    context: context,
                                    firstDate: DateTime(2021),
                                    initialDate: currentValue ?? DateTime.now(),
                                    lastDate: DateTime(2100)))!;
                            if (ref.read(_dateDataProvider.state).state.day !=
                                null) {
                              ref.read(_timeDataProvider.state).state =
                                  (await showTimePicker(
                                context: context,
                                initialTime: TimeOfDay.fromDateTime(
                                    currentValue ?? DateTime.now()),
                              )) as TimeOfDay;
                              return DateTimeField.combine(
                                  ref.read(_dateDataProvider.state).state,
                                  ref.read(_timeDataProvider.state).state);
                            } else {
                              return currentValue;
                            }
                          },
                        ),
                      ),

What I do to get this error is when this is showing i press cancel without selecting all values needed. I want the user to select both date and time mandatory or cancel with no value. I also want the data to be shown in the box after filling it out so the user knows that they have filled it out.

1 Answers

The problem is the null coalescing operator (!) applied to the result of showDatePicker and the cast to TimeOfDay of the showTimePicker result. Just check the result of them first if they are null like the following:

onShowPicker: (context, currentValue) async {
  final date = await showDatePicker(
      context: context,
      firstDate: DateTime(2021),
      initialDate: currentValue ?? DateTime.now(),
      lastDate: DateTime(2100));
  if (date == null) return null;
  ref.read(_dateDataProvider.state).state = date;

  if (date.day != null) {
    final time = await showTimePicker(
      context: context,
      initialTime:
          TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
    );
    if (time == null) return null;
    ref.read(_timeDataProvider.state).state = time;
    return DateTimeField.combine(date, time);
  }

  return currentValue;
},
Related