Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i cant add function to onchanged The argument type 'void Function(Language)' can't be assigned to the parameter type 'void Function(Language?)?'

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

you need to make Language nullable

onChanged: (Language? language) {
  if (language != null) {
    _changeLanguage(language);
  }
},
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading