Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When I add date picker then show 'Non-nullable instance field 'selectedDate' must be initialized. ' error

When I add date picker then show ‘Non-nullable instance field ‘selectedDate’ must be initialized. ‘ error
enter image description here

DateTime selectedDate;

  

    void showDatePicker() {
        showCupertinoModalPopup(
            context: context,
            builder: (BuildContext builder) {
              return Container(
                height: MediaQuery.of(context).copyWith().size.height * 0.25,
                color: Colors.white,
                child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.date,
                  onDateTimeChanged: (value) {
                    if (value != null && value != selectedDate)
                      setState(() {
                        selectedDate = value;
                      });
                  },
                  initialDateTime: DateTime.now(),
                  minimumYear: 2019,
                  maximumYear: 2021,
                ),
              );
            });
      }

widget
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want init variable selectedDate later, use late:

late DateTime selectedDate;

or if you want to make selectedDate can have a null value then initiate it like this:

DateTime? selectedDate;

"?" after data type mean nullable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading