Home > OS >  There should be exactly one item with Dropdownbutton's value
There should be exactly one item with Dropdownbutton's value

Time:01-17

Im very begginer in flutter and I'm trying to put 4 dropdownbuttons and when I change the value for the second one I get error:

There should be exactly one item with [DropdownButton]'s value: Dollars. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value 'package:dropdown_button2/dropdown_button2.dart': package:dropdown_button2/dropdown_button2.dart:1 Failed assertion: line 1128 pos 11: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) { return item.value == value; }).length == 1'

There is the first one working:

String lang = 'English';
 var items = [
   'English',
   'Polish',
 ];
DropdownButtonHideUnderline(
             child: DropdownButton2(
               items: items
                   .map(
                     (item) => DropdownMenuItem<String>(
                       value: item,
                       child: Text(
                         item,
                         style: const TextStyle(
                           fontSize: 14,
                         ),
                       ),
                     ),
                   )
                   .toList(),
               value: lang,
               onChanged: (String? newValue) {
                 setState(() {
                   lang = newValue!;
                 });
               },
               
             ),
           ),

And the second one that isn't

String currency = 'Dollars';
  var curriencies = [
    'Dollar',
    'Euro',
    'PLN',
    'Funts',
  ];
DropdownButtonHideUnderline(
                    child: DropdownButtonFormField2(
                      items: curriencies
                          .map(
                            (currency) => DropdownMenuItem<String>(
                              value: currency,
                              child: Text(
                                currency,
                                style: const TextStyle(
                                  fontSize: 14,
                                ),
                              ),
                            ),
                          )
                          .toList(),
                      value: currency,
                      onChanged: (String? newValue) {
                        setState(() {
                          currency = newValue!;
                        });
                      },

I changed the values, but it's still same

CodePudding user response:

As the error suggest Either zero or 2 or more [DropdownMenuItem]s were detected with the same value. Value of currency variable is Dollars and there is no Dollars in curriencies list hence it gives a null value error. Change the currency value from Dollars to Dollar : -

String currency = 'Dollar';

CodePudding user response:

You are getting that error because, in Flutter drop-down menu, the current value must be the first value in the list of current drop-downs options.

So the first value in your list of dropdown items should be "Dollars" and your variable currency should also be "Dollars".

  • Related