The argument type 'List<DropdownMenuItem<dynamic>>' can't be assigned to the parameter type 'List<DropdownMenuItem<String>>?'

Viewed 807

DropdownButton( value: value, items: locationItems, onChanged: (value) => setState(() { this.value = value; print(value); }), ),

for(int i = 0;i<widget.locationLength;i++){ locationItems.add( DropdownMenuItem( child: Text( widget.locationName ), value: "${widget.locationName}", ) ); }

1 Answers

you need to add type to the dropdown button

DropdownButton<String>( value: value,
 items: locationItems,
 onChanged: (value) => setState(() { 
         this.value = value;
         print(value);
       }),
 ),

and you also must ensure that locationItems is of type List<DropdownMenuItem<String>>?. you can do this by adding type to DropdownMenuItem when adding to list like this:

locationItems.add( DropdownMenuItem<String>( 
   child: Text( widget.locationName ), 
    value: "${widget.locationName}",
   )
);

or by strongly type locationItems when declaring it.

List<DropdownMenuItem<String>>? locationItems;
Related