简体   繁体   English

如何更新 flutter 中的 dropdownMenuItem?

[英]How to update dropdownMenuItem in flutter?

I have 2 dropdowns.我有 2 个下拉菜单。 when selected item from first dropdown.从第一个下拉列表中选择项目时。 I need add items to second dropdown but it giving error.我需要将项目添加到第二个下拉列表,但它给出了错误。

I checked the second dropdown's items is already empty.我检查了第二个下拉列表的项目已经为空。 when first dropdown changed second's dropdown's items is not empty but doesn't work and gived error.当第一个下拉菜单更改时,第二个下拉菜单的项目不为空但不起作用并给出错误。

Any idea?任何想法?

first dropdown code:第一个下拉代码:

Widget getPolWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Icon(
          Icons.touch_app,
          size: 25.0,
          color: Colors.black,
        ),
        SizedBox(width: 50.0),
        DropdownButton(
          items: this.polList
              .map((value) => DropdownMenuItem(
                    child: Text(
                      value.code,
                      style: TextStyle(color: Colors.black),
                    ),
                    value: value.id,
                  ))
              .toList(),
          onChanged: (selectedType) {

          getPod(selectedType);

            setState(() {
              selectedPodId = selectedType;
            });
          },
          value: selectedPodId,
          isExpanded: false,
          hint: Text(
            'Choose Type',
            style: TextStyle(color: Colors.black),
          ),
        )
      ],
    );
  }



Widget getPodWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Icon(
          Icons.touch_app,
          size: 25.0,
          color: Colors.black,
        ),
        SizedBox(width: 50.0),
        DropdownButton(
          items: this.podList
              .map((value) => DropdownMenuItem(                
                    child: Text(
                      value.code,
                      style: TextStyle(color: Colors.black),
                    ),
                    value: value.id,
                  ))
              .toList(),
          onChanged: (selectedType) {
            setState(() {
              selectedPodId = selectedType;
              print(selectedPodId);
            });
          },
          value: selectedPodId,
          isExpanded: false,
          hint: Text(
            'Choose Type',
            style: TextStyle(color: Colors.black),
          ),
        )
      ],
    );
  }

Error message:'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null ||错误消息:'package:flutter/src/material/dropdown.dart':断言失败:第 620 行 pos 15:'items == null || items.isEmpty ||项目.isEmpty || value == null ||值 == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true. items.where((DropdownMenuItem item) => item.value == value).length == 1': 不正确。

Hello man You have to declare one variable and pass into the value parameter in DropdownButton and In the onchanged method you have to update that variable您好,您必须声明一个变量并将其传递给 DropdownButton 中的 value 参数,并且在 onchanged 方法中您必须更新该变量

String a = "fdhsa";
value:a,
onchanged:(h){setState(){a=h;}},

Make sure that your value parameter same from the that list which is inherit in dropdownbutton确保您的值参数与下拉按钮中继承的列表相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM