1

I'm trying to call the showDatePicker widget once an icon button is clicked, however when i call the onTap method get the following error:

The argument type 'Future' can't be assigned to the parameter type '() → void'.

Please find my code below:

showDatePicker code

DateTime selectedDate = DateTime.now();

  Future<Null> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2018, 1),
        lastDate: DateTime(2101));
    if (picked != null && picked != selectedDate)
      setState(() {
        selectedDate = picked;
      });
  }

IconButton code

    IconButton(
        icon: Icon(Icons.date_range),
        onPressed: _selectDate(context)),

I'm using the StatefulWidget, please do let me know how to solve this issue. Thanks :)

Shazly
  • 47
  • 1
  • 2
  • 9

1 Answers1

13

You are calling _selectDate directly, instead of passing a function that calls _selectDate.

Change your onPressed to onPressed: (){_selectDate(context);}

Richard Heap
  • 48,344
  • 9
  • 130
  • 112