Flutter DropdownButton returns error for onChange event
619
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;
});
},
)
Author by
ken4ward
Updated on December 30, 2022Comments
-
ken4ward over 1 year
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.
@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?)?'.