Salatgurke
Salatgurke

Reputation: 2184

The element type 'String' can't be assigned to the list type 'DropdownMenuItem<String>'

I want to fill a DropdownButton in Flutter with some Strings, but I get the error

The element type 'String' can't be assigned to the list type 'DropdownMenuItem<String>' on filing the List with Strings

Here's my Codesnippet:

DropdownButton<String>(
  value: filter.getFilterPersonality(),
  onChanged: (String newValue){filter.setFilterPersonality(newValue);},
  items: ["-"],
),

What am I doing wrong?

Upvotes: 0

Views: 2896

Answers (2)

Kushagra Srivastava
Kushagra Srivastava

Reputation: 411

Items in a DropdownButton should be in a list. Notice how I'm converting my map to a list at the last in the below sample code.

_currencies is an array of strings.

items: _currencies.map((String value) {
                              return DropdownMenuItem<String>(
                                value: value,
                                child: Text(
                                  value,
                                  style: textStyle,
                                ),
                              );
                            }).toList(),

Upvotes: 1

camillo777
camillo777

Reputation: 2367

items should be a List of DropdownMenuItem<String> not a List<String> with just "-".

DropdownButton<String>(
      value: dropdownValue,
      icon: Icon(Icons.arrow_downward),
      iconSize: 24,
      elevation: 16,
      style: TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String newValue) {
        setState(() {
          dropdownValue = newValue;
        });
      },
      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );

See here: https://api.flutter.dev/flutter/material/DropdownMenuItem/DropdownMenuItem.html

Upvotes: 3

Related Questions