ken4ward
ken4ward

Reputation: 2296

Flutter DropdownButton returns error for onChange event

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?)?'.

Upvotes: 2

Views: 1479

Answers (1)

Tirth Patel
Tirth Patel

Reputation: 5736

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;
    });
  },
)

Upvotes: 2

Related Questions