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

flutter The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?' in flutter

The argument type ‘void Function(String)’ can’t be assigned to the parameter type ‘void Function(String?)?’.dartargument_type_not_assignable
String newValue.
hi, i have the above error when trying to implement a dropdown list menu in flutter

bellow is my code. please i am new to flutter


class _CreateAccountState extends State<CreateAccount> {

  String dropdownvalue = 'Apple';
  var items =  ['Apple','Banana','Grapes','Orange','watermelon','Pineapple'];

    @override
  Widget build(BuildContext context) {
    double h = MediaQuery.of(context).size.height;
    double w = MediaQuery.of(context).size.width;
    return Scaffold( ....


 child: DropdownButton(
                value: dropdownvalue,
                  icon: Icon(Icons.keyboard_arrow_down),
                  items:items.map((String items) {
                       return DropdownMenuItem(
                           value: items,
                           child: Text(items)
                       );
                          }
                          ).toList(),
                        onChanged: (String newValue){
                          setState(() {
                            dropdownvalue = newValue;
                          });
                        },
                      ),

thank you all.

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

>Solution :

Errors like this are null-safety related, you can learn more about null-safety here.

If you check out the DropdownButton Class in the official docs you can see an example in which the onChanged property is used:

 onChanged: (String? newValue) {
        setState(() {
          dropdownValue = newValue!;
        });
      },

If you checkout the onChanged property implementation:

final ValueChanged<T?>? onChanged;

The T? means it requires a nullable type, like String? instead of a non-nullable type, like String.

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