1
votes

If someone can please help me with this issue. So I am able to fetch and insert into the dropdownbuttons using the code below. If i select the first dropdown then the list shows accordingly for the second dropdown and if i pick another value the list changes. But when I select between the lists more than once it gives me an error for example:

Another exception was thrown: There should be exactly one item with [DropdownButton]'s value: Ointments.

Either zero or 2 or more [DropdownButton]'s were detected with the same value.

I assume this is because I'm calling the functions inside the onChanged: of the buttons. Any help would be much appreciated. Thanks!

note: I have also put only the dynamicDropDownMainCategory() function in initState.

//FIRST DROPDOWNBUTTON BELOW
//

child: DropdownButton<String>(
                            hint: Text('Select Category'),
                            icon: Icon(Icons.keyboard_arrow_down),
                            iconSize: 28,
                            isDense: true,
                            isExpanded: true ,
                            elevation: 16,
                            style: TextStyle(
                              color: Colors.black,
                              fontSize: 16.0,
                            ),
                            value: categoryManualCurrent.isNotEmpty ? categoryManualCurrent : null,
                            onChanged: (String categoryValue) async {

                             //  await dynamicDropDownMainCategory();

                              setState(() {

                                mainCategoryCurrent = categoryValue;
                                categoryManualCurrent = categoryValue;

                              }); 

                                        print('THIS' + categoryManual.toString());
                                        print('THAT' + subCategoryManual.toString());

                                await dynamicDropDownSubCategory();
                            },
                            items: categoryManual.toList()
                              .map((var value3) {
                                return  DropdownMenuItem<String>(
                                  value: value3,
                                  child: Text(value3),
                                );
                               }).toList(),
                             ),
                           )   
                          ],
                          ),


                    //SECOND DROPDOWNBUTTON BELOW
                    //

                          child: DropdownButton<String>(
                            hint: Text('Select subCategory'),
                            icon: Icon(Icons.keyboard_arrow_down),
                            iconSize: 28,
                            isDense: true,
                            isExpanded: true ,
                            elevation: 16,
                            style: TextStyle(
                              color: Colors.black,
                              fontSize: 16.0,
                            ),

                            value: subCategoryManualCurrent.isNotEmpty ? subCategoryManualCurrent : null,
                            onChanged: (String subCategoryValue) async {

                            //await dynamicDropDownMainCategory();
                             await dynamicDropDownSubCategory();

                                subCategoryCurrent = subCategoryValue;
                               setState(() {
                                 subCategoryManualCurrent = subCategoryValue;
                               }); 

                            //   await  dynamicDropDownSubCategory();

                            },
                              items: subCategoryManual.toList()
                              .map((var value1) {
                                return DropdownMenuItem<String>(
                                  value: value1,
                                  child: Text(value1),
                                );
                               }).toList(),
                             ),
                           ),
                      ]
                          ),


//TWO functions im using to call the list data from firestore and insert into the //dropdownbutton

 Future dynamicDropDownMainCategory() async{

      await Firestore.instance

              .collection('Products')
              .document('Categories')                                    
              .get()                                                             
              .then((snapshot) => {  

              categoryManual = (snapshot.data['mainCategory']),

              }
              );
             }


    Future dynamicDropDownSubCategory() async{

          await Firestore.instance

              .collection('Products')
              .document('Categories')                                    
              .get()                                                             
              .then((snapshot) => {  

    if (mainCategoryCurrent == categoryManualCurrent){
              subCategoryManual = (snapshot.data['subCategory'+' '+mainCategoryCurrent]),

           } else {

            subCategoryManual = ['Error Getting Data'],

              }
              }
          );
    }

2

2 Answers

1
votes

this error happens when u have two strings that are equal & u set it as value to dropdowmbutton as it need to be unique :

return DropdownMenuItem(
  value: data.categoryName,
  child: Text(data.categoryName, style: Constants.normalLarge),
);

in above example the value property need to be unique so that it can render appropriate item.

0
votes

this is happening because the dependent lists has different sizes, so basically parent 1 has a dependent dropdown with 4 elements, you select the last [index 3]. Then you change to parent 2 which has a dependent dd with 3 elements, what happens is the dependat dropdown is having the index 3 still as you only have changed the parent, and since parent 2 dependent dd goes only to index 2 it gives this error.

Hope I have helped.