Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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.

like image 534
Yusuf Avatar asked Jul 26 '26 06:07

Yusuf


2 Answers

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.

like image 131
Jahn E. Avatar answered Jul 28 '26 14:07

Jahn E.


The argument type of onChanged is void Function(String?). So you can't assign a function of argument type void Function(String)

So please change the code as following:

(change the String type to String? in "onChanged" argument

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;
                         });
                       },
                     ),
like image 20
Uvais Avatar answered Jul 28 '26 15:07

Uvais



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!