skip to Main Content

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


  1. Chosen as BEST ANSWER

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


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search