Home > other >  How to use DropdownButton without setting an initial select in flutter?
How to use DropdownButton without setting an initial select in flutter?

Time:02-01

Hi I'm new to Flutter.

I had been getting an exception There should be exactly one item with [DropdownButton]'s value: . when using DropdownButton class. And it was resolved once by setting an initial select referring to this enter image description here

CodePudding user response:

There should be exactly one item with [DropdownButton]'s value: means you are having same value more than one DropdownMenuItem. You can convert the data-list to Set, most time it works.

Here the items is

 List<String> items = [...];
 String? selectedItem;

And the dropDownButton

DropdownButton<String?>(
  items: items
      .toSet()
      .map(
        (e) => DropdownMenuItem<String?>(
          value: e,
          child: Text(e),
        ),
      )
      .toList(),
  onChanged: (value) {},
)
  • Related