2

I'm writing a project and wants to add a DropDownButton but it always return error once I add onCnange event. Dropdown always returns error. I don't know what's wrong with it.enter image description here

@override
  Widget build(BuildContext context) {
    return Material( child: Column( children: [
      DropdownButton(items: [], value: dropdownValue, onChanged: (String value)  {
        setState(() {
          dropdownValue = value;
        });
      },)
    ], ), );
  }

Error message

The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(Object?)?'.
ken4ward
  • 2,246
  • 5
  • 49
  • 89

1 Answers1

2

function and value might be nullable, so either remove String type from the callback or use the nullable type.

Solution 1

DropdownButton<String>(
  items: [],
  value: dropdownValue,
  onChanged: (value) {
    setState(() {
      dropdownValue = value;
    });
  },
)

Solution 2

DropdownButton<String>(
  items: [],
  value: dropdownValue,
  onChanged: (String? value) {
    setState(() {
      dropdownValue = value;
    });
  },
)
Tirth Patel
  • 5,443
  • 3
  • 27
  • 39