首页 > 解决方案 > Flutter - Dependent/Multilevel DropdownButton 有一个问题:应该只有一项具有 [DropdownButton] 的值:Ointments

问题描述

如果有人可以帮助我解决这个问题。所以我可以使用下面的代码获取并插入下拉按钮。如果我选择第一个下拉列表,则列表会相应地显示第二个下拉列表,如果我选择另一个值,则列表会更改。但是,当我在列表之间进行多次选择时,它会给我一个错误,例如:

引发了另一个异常:应该只有一个具有 [DropdownButton] 值的项目:Ointments。

检测到具有相同值的 0 个或 2 个或多个 [DropdownButton]。

我认为这是因为我正在调用按钮的 onChanged: 内的函数。任何帮助将非常感激。谢谢!

注意:我还在 initState 中只放置了 dynamicDropDownMainCategory() 函数。

//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'],

              }
              }
          );
    }

标签: flutterdartdropdownbutton

解决方案


当您有两个相等的字符串并且您将其设置为 dropdowmbutton 的值时会发生此错误,因为它需要是唯一的:

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

在上面的示例中, value 属性需要是唯一的,以便它可以呈现适当的项目。


推荐阅读