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

Posted by


There should be exactly one item with [DropdownButton]'s value: Argentina. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
Failed assertion: line 1578 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'


The error you’re encountering suggests that there’s an issue with the DropdownButton and its selected value. It seems that there might be duplicate values in the list of items for the DropdownButton.

Here are steps to resolve the issue:

Step 1. Check colleges List: Ensure that the colleges list does not contain duplicate values.

List colleges = [];

Step 2: Initialize _dropdownvaluecolleges in initState: In the initState method, ensure that _dropdownvaluecolleges is initialized with a valid value and that it exists in the colleges list.

_dropdownvaluecolleges = colleges.contains(widget.profileEditData['college'])
    ? widget.profileEditData['college']
    : "Select college"; // Set a default value if necessary

Step 3: Print Statements for Debugging: Add print statements to debug and print the values of colleges, _dropdownvaluecolleges, and widget.profileEditData['college']:

print("Colleges: $colleges");
print("Selected college: $_dropdownvaluecolleges");
print("Profile college: ${widget.profileEditData['college']}");

Examine the output in the console for any unexpected values.

Step 4: Check for Duplicate Items in Dropdown: Ensure that when creating the DropdownButton, the list of items does not contain duplicates.

Verify that each item in colleges has a unique name.

Step 5: Update _dropdownvaluecolleges on Dropdown Change: If the dropdown allows users to change the selection, make sure to update the _dropdownvaluecolleges variable when the dropdown value changes.

Once these steps have been followed and any necessary adjustments have been made, the problem ought to be fixed. Please provide the pertinent sections of your code, particularly the definition and initialisation of the dropdown, if the issue continues.

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x