Flutter_form_builder: (3.3.4) FormBuilderDateTimePicker ignoring initialDate

Created on 9 Aug 2019  路  6Comments  路  Source: danvick/flutter_form_builder

Hi Danvick,

Noticed a possible breaking change.

Passing initialDate: DateTime.tryParse("20000101"), for example, in the constructor has no effect. The date picker still open at current date.

And if one passes a lastDate that is before today, then one gets an
!initialDate.isAfter(lastDate) assertion error from within date_picker.dart

The problem seems to be here in form_builder_date_time_picker.dart

Future<DateTime> _showDatePicker(
     ....
      return showDatePicker(
          context: context,
          ....
          initialDate: currentValue ?? DateTime.now(),
          firstDate: widget.firstDate ?? DateTime(1900),
          lastDate: widget.lastDate ?? DateTime(2100));
  }

I think initialDate: (currentValue ?? widget.initialDate) ?? DateTime.now() is what's required. But you can confirm.

Thanks
Abhinav.

bug

Most helpful comment

Hi,
The initialValue also not working in FormBuilderDateTimePicker. The value set in initialValue i.e {'date':DateTime.utc(2010,1,1)} were successfully assigned to the attribute (it is not null) but not printed in the field, it just blank.

Edited :
...and when _formkey.currentstate.reset() fired, the field filled with initialValue.

All 6 comments

Thank you for bringing this to our attention.

Hi,
The initialValue also not working in FormBuilderDateTimePicker. The value set in initialValue i.e {'date':DateTime.utc(2010,1,1)} were successfully assigned to the attribute (it is not null) but not printed in the field, it just blank.

Edited :
...and when _formkey.currentstate.reset() fired, the field filled with initialValue.

Looks like the culprit is that the value of the text editing controller is not being set on intiState.

Adding the following got me back up and running:

In initState(), right after
_textFieldController = widget.controller ?? TextEditingController();

widget.initialValue == null ? "" :
widget.format == null ? DateFormat("EEEE, MMMM d, yyyy 'at' h:mma").format(
    widget.initialValue) :
widget.format.format(widget.initialValue));

I'm sure there is a more elegant way to do this - also, I have a check box in my app to show or hide the date, that logic seems to be not working at the moment.

This bug has come sometime after 3.0.1. Hence I am hoping it gets fixed here itself.

Hi @abhinavc,
Kindly confirm that this issue is fixed as of v3.4.0.
Feel free to reopen the issue if not.

Hi Danvick,
Tried 3.4.1 and it seems the issue still remains.
firstDate is honored. lastDate is also honored, though specifying one can lead to an assertion
error because initialDate still seems to be set to today's date.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

awhitford picture awhitford  路  5Comments

joshuakoh1 picture joshuakoh1  路  3Comments

SachinTanpure picture SachinTanpure  路  3Comments

awhitford picture awhitford  路  5Comments

WilliamCunhaCardoso picture WilliamCunhaCardoso  路  3Comments