'Error: Either zero or 2 or more [DropdownMenuItem]s were detected with the same value I/flutter (18363): 'package:flutter/src/material/dropdown.dart':

Error code Hi I'm new to flutter and have a question about dropdownbutton regarding using the same values for multiple dropdownbutton.

From my understanding from the error, it was due to using the same list for 2 or more dropdownbuttons in the same activity.

How am i able to resolve this error but still able to reuse the list for 2 or more dropdownbuttons?

  String _value1;
  String _value2;

  final List<String> nameList = <String>[
    "Name1",
    "Name2",
    "Name3",
    "Name4",
    "Name5",
    "Name6",
    "Name7",
    "Name8"
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 2.0,
        title: Text('Hello'),
      ),
      body:  ListView(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Text('Name: '),
                  Center(
                    child: DropdownButton(
                      value: _value1,
                      onChanged: (value) {
                        setState(() {
                          _value1 = value;
                        });
                      },
                      items: nameList.map(
                        (item) {
                          return DropdownMenuItem(
                            value: item,
                            child: new Text(item),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                ],
              ),
              Row(
                children: <Widget>[
                  Text('Name: '),
                  Center(
                    child: DropdownButton(
                      value: _value2,
                      onChanged: (value) {
                        setState(() {
                          _value2 = value;
                        });
                      },
                      items: nameList.map(
                        (item) {
                          return DropdownMenuItem(
                            value: item,
                            child: new Text(item),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                ],
              ),
            ],
          ),
      backgroundColor: Colors.grey[200],
    );
  }
}


Solution 1:[1]

I had the exact same error, multiple Dropdowns all feeding from the same static list, the only difference is that in my case, it was a list of Objects, not Strings.

So, if it's a static list, there's no way it's empty, no duplicate values in the list, AND you already make sure value is not empty? Then the only option remaining is that item.value is different than value

In my case, as it was an Object list, I had to overwrite operator == and hashcode methods in my Object class.

bool operator ==(dynamic other) =>
      other != null && other is TimeSelection && this.hour == other.hour;

  @override
  int get hashCode => super.hashCode;

And that was it. I didn't had to initialize _value1 or _value2

Solution 2:[2]

This exception you have because of mistakes:

  1. No _value1 and _value2 initialization.
  2. When you initialize them make sure that _value1 and _value2 right from nameList e.g.
_value1 = nameList[0];
_value2 = nameList[3];

this is important step with complex data type, but in your case

_value1 = "Name1";
_value2 = "Name4";

will be sufficient.

Full example:

  String _value1;
  String _value2;

  final List<String> nameList = <String>[
    "Name1",
    "Name2",
    "Name3",
    "Name4",
    "Name5",
    "Name6",
    "Name7",
    "Name8"
  ];

  /// initialization is here:
  @override
 void initState() {
    super.initState();
    _value1 = nameList[0];
    _value2 = nameList[3];
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        Row(
          children: <Widget>[
            Text('Name: '),
            Center(
              child: DropdownButton(
                value: _value1,
                onChanged: (value) {
                  setState(() {
                    _value1 = value;
                  });
                },
                items: nameList.map(
                  (item) {
                    return DropdownMenuItem(
                      value: item,
                      child: new Text(item),
                    );
                  },
                ).toList(),
              ),
            ),
          ],
        ),
        Row(
          children: <Widget>[
            Text('Name: '),
            Center(
              child: DropdownButton(
                value: _value2,
                onChanged: (value) {
                  setState(() {
                    _value2 = value;
                  });
                },
                items: nameList.map(
                  (item) {
                    return DropdownMenuItem(
                      value: item,
                      child: new Text(item),
                    );
                  },
                ).toList(),
              ),
            ),
          ],
        ),
      ],
    );
  }
}

Solution 3:[3]

You are getting that exception because _value1 and _value2 aren't initialized and providing empty to the dropdown widget.

You could do something like this:

DropdownButton(
  value: _value1.isNotEmpty ? _value1 : null, // guard it with null if empty
  items: nameList.map((item) {
           return DropdownMenuItem(
              value: item,
              child: new Text(item),
           );
         }).toList(), 
);

Solution 4:[4]

I have the same problem, and I solved it. The dropdown button needs items list and value. We define items and selected items, but the item chosen instance does not inside the items list.

You should try this and fix your logic. (value ?s selected item value for user)

 var _value = itemList.isEmpty
    ? value
    : itemList.firstWhere((item) => item.value == value.value);

More : https://gist.github.com/VB10/fd560694fec0a38751e798a213408001

Solution 5:[5]

You must initialise the _value1 and _value2 and make sure those values are also present in nameList.

Solution 6:[6]

My solution was more simple than every one else. The fact that was find a value that wasn't the same as in the list, is because I have put a value in the variable, that wasn't either full or empty, the value was this ("") and it has to be null for the Dropdown value instance. So, I just have put a value null in the declaration of variable. like: "String _value;", and voila, it worked.

#Sorry for the English, Brazilian here.

Solution 7:[7]

You must initialise the _value1 and _value2 with a initial Value.

Solution 8:[8]

_value1 and _value2 variables need to be initialized, or you can do that:

value: _value1 != null ? _value1 : null,
hint: Text('Your hint'),

Solution 9:[9]

var _issues = [ "Subscription Related", "Talk Therapy Related", "Program Related", "Account Related", "Technology Related", ];

String _currentSelectedValue=_issues.first;

Solution 10:[10]

I had same issue with Getx package. When it's updated, it causes this error because add same items to list. Adding key: UniqueKey() to DropdownButtonFormField is the solution for me.

Solution 11:[11]

In my case, I use FormBuilderDropdown of the package flutter_form_builder. Adding key: UniqueKey() in the Widget FormBuilderDropdown is the solution for my case.

Solution 12:[12]

As much as the answer accepted may be working, it is unnecessary and over achieving.

All you need to do is ENSURE THAT whatever your initialized the value to is in the list as well. That is:

String _value1 = "Name1";

If the initialized value is not in your list, you will get the error message you are getting. Period!

Solution 13:[13]

If you are sure that your code is right then do "hot restart" instead of "hot reload".

Solution 14:[14]

You can use the same list in multiple DropDownButton. The error you got is because of having more than one same values in the list. For Example, if I change the list to given below where I have two items having the same value, it will throw me an error.

`final List<String> nameList = <String>[
    "Name1",
    "Name1",
    "Name3",
    "Name4",
    "Name5",
    "Name6",
    "Name7",
    "Name8"
  ];`

Error: enter image description here

Solution 15:[15]

_value1 and _value2 must be in your list

Solution 16:[16]

this solved in my case

value: _value1,
    onChanged: (value) {
      setState(() {
        _value1 = value;
        _value2 = null;
      });
    },

Solution 17:[17]

The dropdownbuttonformfield filtering mechanism uses a single field for filtering. It can not filter by class hash. Set the DropdownMenuItem value to the key of the incoming data. The dropdownmenuItem value must be unique. The key is String type. currentStatus holds the key to position in the dropdown. I map a list of class objects where the class has databaseValue field and a displayValue field to the dropdownmenuitem as value:databaseValue and child:Text(displayValue). Now, I can set _currentStatus to a databaseValue and it will position in the dropdown.

String _currentStatus;

List<DropdownMenuItem> listMenuItems =
  <DropdownMenuItem<String>>[];

Provider.of<Api>(context, listen: false)
    .getComboViews()
    .then((data) {
  setState(() {
    listMenuItems =
        data.map<DropdownMenuItem<String>>((item) {
      return DropdownMenuItem<String>(
          value: item.databaseValue, child: Text(item.displayValue));
    }).toList();


 DropdownButtonFormField<String>( 
     value: this._currentStatus,
     items: listMenuItems
     onChanged: (String value) {
           setState(() {
           this._currentStatus = value;
    });
  );

Solution 18:[18]

String? dropdownValue
 hint: Text( "Select City"),
  value: dropdownValue == null ? null : dropdownValue,


onChanged: (String? newValue) {
                      setState(() {
                        dropdownValue = newValue;
                      });
                    },
                    items: <String>[
                      'Islamabad',
                      'RawalPindi',
                      'Mangla',
                      'Mirpur'
                    ].map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,
                        child: Text(value),
                      );
                    }).toList(),

Solution 19:[19]

Once you have Multiple DropDownButtons which are dependent on one another. Adding key: (_value1 != null) ? Key(_value1) : UniqueKey() to the dependent DropdownButtonFormField

children: <Widget>[
              Row(
                children: <Widget>[
                  Text('Name: '),
                  Center(
                    child: DropdownButton(
                      value: _value1,
                      onChanged: (value) {
                        setState(() {
                          _value1 = value;
                        });
                      },
                      items: nameList.map(
                        (item) {
                          return DropdownMenuItem(
                            value: item,
                            child: new Text(item),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                ],
              ),
              Row(
                children: <Widget>[
                  Text('Name: '),
                  Center(
                    child: DropdownButton(
                      value: _value2,
                      key: (_value1 != null) ? Key(_value1) : UniqueKey()
                      onChanged: (value) {
                        setState(() {
                          _value2 = value;
                        });
                      },
                      items: nameList.map(
                        (item) {
                          return DropdownMenuItem(
                            value: item,
                            child: new Text(item),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                ],