1

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

1

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.

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

6 Comments

Hey GJJ2019, thanks for helping me out. I tried this but still no luck. Can you maybe give me an example with the variable names I am using in my code. I still get the same error.
check out Oinment value for dropdown. I also got same error way back i resolve it by providing unique value its complaining about that only if your code works. Tip: dont fetch value from database whenver database changes thats not ideal use map instead it ll be helpful
Still I have the same issue. Can you please help me out. Been trying this for 2 days.
debug it print all values that u are passing to DropDownButton
Ya so i did that and noticed that ofcourse value was getting passed again. But if I change the value property like u said, it doesnt crash with the error, but the drop down list is empty, but it still holds the value. But doesnt display. So thats another issue :( any suggestions? thanks for your time.
|
1

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.

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.