【问题标题】:Flutter - Dependent/Multilevel DropdownButton has an issue: There should be exactly one item with [DropdownButton]'s value: OintmentsFlutter - Dependent/Multilevel DropdownButton 有一个问题:应该只有一项具有 [DropdownButton] 的值:Ointments
【发布时间】:2020-07-18 23:20:16
【问题描述】:

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

引发了另一个异常:应该恰好有一项具有 [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'],

              }
              }
          );
    }

【问题讨论】:

    标签: flutter dart dropdownbutton


    【解决方案1】:

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

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

    在上面的例子中, value 属性需要是唯一的,这样它才能呈现适当的项目。

    【讨论】:

    • 嘿 GJJ2019,感谢您帮助我。我试过这个,但仍然没有运气。你能不能给我一个我在代码中使用的变量名的例子。我仍然遇到同样的错误。
    • 检查 Oinment 值的下拉菜单。我也遇到了同样的错误,我通过提供唯一值来解决它,只有当你的代码有效时才会抱怨它。提示:当数据库更改不理想时,不要从数据库中获取值,而是使用 map 会有所帮助
    • 我还是有同样的问题。你能帮帮我吗?尝试了 2 天。
    • 调试它打印你传递给DropDownButton的所有值
    • 是的,所以我这样做了,并注意到当然价值再次被传递。但是,如果我像你说的那样更改 value 属性,它不会因错误而崩溃,但下拉列表是空的,但它仍然保存该值。但不显示。所以这是另一个问题:(有什么建议吗?感谢您的宝贵时间。
    【解决方案2】:

    这是因为依赖列表的大小不同, 所以基本上父 1 有一个包含 4 个元素的依赖下拉列表,您选择最后一个 [索引 3]。然后您更改为具有 3 个元素的依赖 dd 的父 2,发生的情况是依赖下拉列表仍然具有索引 3,因为您只更改了父级,并且由于父级 2 依赖 dd 仅转到索引 2,因此会出现此错误.

    希望我有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2021-08-29
      • 2020-06-16
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 2023-01-17
      • 2023-01-05
      • 2020-08-26
      相关资源
      最近更新 更多