AthensRR18
AthensRR18

Reputation: 37

The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?'

Widget _buildDropDownButton(String currencyCategory) {
return DropdownButton(
  value: currencyCategory,
  items: currencies
      .map((String value) => DropdownMenuItem(
    value: value,
    child: Row(
      children: <Widget>[
        Text(value),
      ],
    ),
  ))
      .toList(),
  onChanged: (String value) {
    if(currencyCategory == fromCurrency){
      _onFromChanged(value);
    }else {
      _onToChanged(value);
    }
  },
);

}

Hi, I have an issue in this part of sample code, in this part of code:

      onChanged: (String value) {
    if(currencyCategory == fromCurrency){
      _onFromChanged(value);
    }else {
      _onToChanged(value);
    }
  },

It shows an error The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?'

I tried placing final void Function() onChanged; or final VoidCallback onChanged; but it expects and identifier. Is there something that I missed?

Upvotes: 1

Views: 1143

Answers (1)

Emad Adly
Emad Adly

Reputation: 131

This is because of the null safety, the value for the dropdown can be null when nothing is selected. Add a "?" after the String type declaration to be like onChanged(String? value) {//on change logic}.

Checkout the flutter DropdownButton example.

Upvotes: 2

Related Questions