1

I have two DropdownButtonFormfield on my page. They have relation. The second is filling after select an item from first. I am facing an error on this situation: Select an item from first, select an item from second, change the first value. I am seeing this error after apply this steps.

There should be exactly one item with [DropdownButtonFormField]'s value: 1789. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
dropdown.dart:1
Failed assertion: line 933 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

My code :

DropdownButtonFormField<int?>(
        value: userController
         .selectedReturnCity,
        items: List.generate(
                  userController.storeCityFilter?.length ?? 0,
                   (index) => DropdownMenuItem(
                                         value: userController.storeCityFilter![index]['id'],
                                         child: Text(userController
                                                      .storeCityFilter![index]['value']))),
        onChanged: (value) async {
                      userController.selectedReturnCity = value == 0 ? null : value;
                      //Get data for second dropdown, the function is clears the data used by the second dropdown
                      await userController.getStores(city: userController.selectedReturnCity,
                                                  type: "market");
                  },
           ),
           10.v(),
DropdownButtonFormField<int?>(
        value: userController.selectedReturnStore,
        items: List.generate(
                  userController.storeList?.length ?? 0,
                   (index) => DropdownMenuItem<int?>(
                                         value: userController.storeList?[index].id,
                                         child: Text(userController.storeList?[index].name ?? ''))),
onChanged: (value) {userController.selectedReturnStore = value;
},
)

2 Answers 2

1

This situation happens when you try to set value: userController.selectedReturnCity and the value of userController.selectedReturnCity doesn't exist in your list or there are two or more same items/objects in your list and try to set that value in userController.selectedReturnCity.

Because the value property of DropdownButtonFormField means which item will be selected in your dropdown. Please debug both of your dropdowns.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answer. My error on second Dropdown and selectedReturnStore. I am updating null to selectedReturnStore on userController.getStores func's first line.
Your answer not working for me. The error keep shows.
So when you're selecting the first dropdown for the second time, you got this error on the second dropdown, right?
Yes, right. My second dropdown give error after update first value.
It's all about the data you're providing in the list and what you're setting value property. You have to give a proper explanation with your provided data. Based on your error I can mention one thing which is you're setting the value with id 1789 which is not present in your list.
0

I resolved with selectedReturnStore set to 0. I can't explain to myself :D bullshit..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.