0

can't get the date picked from my datepicker though i didn't getting error on my code.

  Future selectDate() async {
    DateTime picked = await showDatePicker(
        context: context,
        initialDate: new DateTime.now(),
        firstDate: new DateTime(2016),
        lastDate: new DateTime(2222));
    if (picked != null) setState(() => picked = datepicked);
    else{
      print(picked);
    }
  }
}

enter image description here

1 Answer 1

1

You need to pass the context in the datepicker method. Complete working code below:

body: Center(
        child: TextButton(
        child: Text('click'),
        onPressed: () {
          selectDate(context);
          }
        )
      ),


Future selectDate(BuildContext context) async {
    DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: new DateTime(2016),
        lastDate: new DateTime(2222));
    if (picked != null && picked!= selectedDate) {
      setState(() => selectedDate = picked);
      print(picked);
    }
    else{
      print(picked);
    }
  }

Result:

2020-02-21 00:00:00.000
Sign up to request clarification or add additional context in comments.

2 Comments

yeah, that was good answer too.sorry, I've just panic. my code is working ,I just uninstall my application and restart the hot reload.(i didn't know what was happended lately) by the way, thanks for your answer :)
picked != null :The operand can't be null, so the condition is always true. You try: if (picked!= selectedDate) { .....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.