Home > Back-end >  flutter The argument type 'void Function(String)' can't be assigned to the parameter
flutter The argument type 'void Function(String)' can't be assigned to the parameter

Time:12-03

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.

CodePudding user response:

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.

CodePudding user response:

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;
                         });
                       },
                     ),
  • Related