Error:
The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?'.dartargument_type_not_assignable
abstract final class String implements Comparable<String>, Pattern
dart:core
Solution:
The error occurs because the onChanged callback in DropdownButtonFormField expects a function that takes a nullable string (String?), but your onChanged callback is specified to take a non-nullable string (String). To resolve this, you can update the type of newValue in your onChanged callback to be nullable:
DropdownButtonFormField<String>(
isExpanded: true,
icon: const Icon(Icons.keyboard_arrow_down),
value: selectedDegree,
hint: Text(
'Type & Select Degree'), // Displayed when no option is selected
items: degreeOptions.map((String degree) {
return DropdownMenuItem<String>(
value: degree,
child: Text(degree),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
selectedDegree =
newValue ?? ''; // Update the selected degree when an option is chosen
});
},
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please select your Degree';
}
return null;
},
),
In this updated code, newValue is now nullable (String?) to match the expected parameter type of onChanged. The ?? '' operator is used to provide a default value ('') in case newValue is null.

Leave a Reply