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
I resolved with selectedReturnStore set to 0. I can't explain to myself :D bullshit..
This situation happens when you try to set
value: userController.selectedReturnCity
and the value ofuserController.selectedReturnCity
doesn’t exist in your list orthere are two or more same items/objects in your list and try to set that value in
userController.selectedReturnCity
.Because the
value
property ofDropdownButtonFormField
means which item will be selected in your dropdown. Please debug both of your dropdowns.