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 Answers2

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.

Gazi Mohib
  • 71
  • 8
  • Thanks for answer. My error on second Dropdown and selectedReturnStore. I am updating null to selectedReturnStore on userController.getStores func's first line. – Baran Güngör Jun 16 '23 at 12:45
  • You're welcome. Please upvote my answer if you can. – Gazi Mohib Jun 16 '23 at 13:11
  • Your answer not working for me. The error keep shows. – Baran Güngör Jun 16 '23 at 13:15
  • So when you're selecting the first dropdown for the second time, you got this error on the second dropdown, right? – Gazi Mohib Jun 16 '23 at 13:45
  • Yes, right. My second dropdown give error after update first value. – Baran Güngör Jun 16 '23 at 13:55
  • 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. – Gazi Mohib Jun 16 '23 at 14:07
0

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