I'm trying to make an Internationalizing app with this youtube movie: https://www.youtube.com/watch?v=yX0nNHz1sFo&list=PLyHn8N5MSsgEfPAxCytQDPATDlHwpP5rE&index=4
When I add a function to DropDownButton's onChanged I get the error: The argument type 'void Function(Language)' can't be assigned to the parameter type 'void Function(Language?)?'
Here is the code
class GeneralList extends StatefulWidget {
GeneralList({super.key});
@override
State<GeneralList> createState() => _GeneralListState();
}
class _GeneralListState extends State<GeneralList> {
void _changeLanguage(Language language) {
print(language);
}
@override
Widget build(BuildContext context) {
return Container(
child: DropdownButton2(
onChanged: (Language language) {
_changeLanguage(language);
},
items: Language.languageList()
.map<DropdownMenuItem<Language>>(
(lang) => DropdownMenuItem(
value: lang,
child: Row(
children: <Widget>[Text(lang.name)],
),
),
)
.toList()),
);
}
}
I looked up for an answer in this post on stack: Can't add a function to onChanged property
but I still get the same error
CodePudding user response:
you need to make Language
nullable
onChanged: (Language? language) {
if (language != null) {
_changeLanguage(language);
}
},