flutter dropdown error There should be exactly one item with [DropdownButton]’s value: 1

In this post, i will tell to you how to solve error Flutter on dropdown widget.

One of generic error message coming from Flutter when you use dropdown widget is.

There should be exactly one item with [DropdownButton]’s value: 1.

Either zero or 2 or more [DropdownMenuItem]s were detected with the same value

with the detail error like below

flutter dropdown error - There should be exactly one item with [DropdownButton]'s value: 1

Error above coming when you try to select item in dropdown menu onChange event. Some people might be confuse why that’s error comes up, event thought the value were selected is correct but that’s error keep showing.

Commonly the error cause of your type value from variable you’ve declare is different with the type value from selected item.

So here is step by step how to solve the error:

  1. Check what type of variable inside list item that you use for Dropdown Widget.
  2. Declare variable that you use for storing change item with the same type of variable in list item.

Here is the example, i have class named DropdownClass with 2 property id as integer and value as string,

class DropdownClass {
  final int id;
  final String value;
  
  DropdownClass(this.id, this.value);
}

After that, i declaring variable List of DropdownClass, and add some value to the list

In code above, you can see there is variable that i declare, _idDepartment as String. That variable will be handling selected item on event Change. If you notic the variable _idDepartment have different type with id inside DropdownClass.

Next step, let’s just call them in dropdown widget.

Padding(
                  padding: const EdgeInsets.only(top: 20.0, right: 45.0, left: 45.0),
                    child: new FormField(
                    builder: (FormFieldState state) {
                      return InputDecorator(
                        decoration: InputDecoration(
                          icon: const Icon(Icons.business),
                          labelText: 'Department',
                        ),
                        child: new DropdownButtonHideUnderline(
                          child: new DropdownButton(
                            isDense: true,
                            onChanged: (newValue) {
                              setState(() {
                                _idDepartment= newValue.toString();
                              });
                            },
                            items: _department.map((item) {
                              return DropdownMenuItem(
                                child: Text(item.value),
                                value: item.id,
                              );
                            }).toList(),
                            value: _idDepartment,
                          ),
                        ),
                      );
                    },
                  ),
                ),

Once you run your poject, you can see error on your screen.

Flutter dropdown error

As a result, error comes up. That’s what happen when you have different type variable for handle item value.

Let’s just fix it…!!!

because we want to use id in value Dropdown Widget and we put it into _idDepartment variable, so just change type variable string _idDepartment to integer, pretty much like type id integer in DropdownClass.

int _idDepartment;

Now, run your project again, and you will see the error was disappeared from screen.

Flutter dropdown error solved

GREATTT, you fixed that.

Hope this post help your problem.

Please, check this link for more tutorial in Flutter.

Advertisements
Share: