Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振中的下拉按钮不将值更改为选定值_Flutter_Dart_Dropdown - Fatal编程技术网

Flutter 颤振中的下拉按钮不将值更改为选定值

Flutter 颤振中的下拉按钮不将值更改为选定值,flutter,dart,dropdown,Flutter,Dart,Dropdown,在我的代码中,我添加了一个下拉列表,如下所示,当我在下拉列表中切换选择时,它没有得到更新,它显示异常,我在statefull小部件中声明了一个变量,在我的下拉函数中,我将其作为值分配给下拉按钮,在Onchanged中,我将json传递给另一个函数,在那里我从一个变量中获取值,并将其分配给setState中的一个optselected变量 class _ReportFilterState extends State<ReportFilter> { String opSelecte

在我的代码中,我添加了一个下拉列表,如下所示,当我在下拉列表中切换选择时,它没有得到更新,它显示异常,我在statefull小部件中声明了一个变量,在我的下拉函数中,我将其作为值分配给下拉按钮,在Onchanged中,我将json传递给另一个函数,在那里我从一个变量中获取值,并将其分配给setState中的一个optselected变量

class _ReportFilterState extends State<ReportFilter> {

  String opSelected;
 //declared string to hold the selected value in dropdown within the state class.

    buildMainDropdown(List<Map<String, Object>> items, StateSetter setState) {
        return Container(
          child: Padding(
            padding: const EdgeInsets.symmetric(
              horizontal: 27.0,
              vertical: 16.0,
            ),
            child: Align(
              alignment: Alignment.topLeft,
              child: DropdownButtonHideUnderline(
                child: DropdownButton(
                  isExpanded: true,
                  hint: Text("Choose Filters"),
                  value: opSelected, // Here assigning the value 
                  items: items
                      .map((json) => DropdownMenuItem(
                          child: Text(json["displayName"]), value: json))
                      .toList(),
                  onChanged: (json) {
                    manageIntState(json, setState);
                  },
                ),
              ),
            ),
          ),
        );
      }

 void manageIntState(Map<String, Object> jsonSelected, StateSetter setState) {
    setState(() {
      dispName = jsonSelected["displayName"]; 

//here I am setting the selected value
      opSelected = dispName;

//Doing some operations
      id = jsonSelected['id'];
      type = jsonSelected['type'];
      selectedFilterOption = jsonSelected;

      if (jsonSelected.containsKey("data")) {
        List<Map<String, Object>> tempList;
        List<String> dailogContent = List<String>();
        tempList = jsonSelected['data'];

        tempList
            .map((val) => {
                  dailogContent.add(val['displayId']),
                })
            .toList();
        _showReportDialog(dailogContent);
      }
    });
  }
class\u报告过滤器状态扩展状态{
所选字符串;
//声明的字符串,用于在状态类的下拉列表中保存选定的值。
BuildMain下拉列表(列表项、状态设置器设置状态){
返回容器(
孩子:填充(
填充:const EdgeInsets.symmetric(
水平线:27.0,
垂直线:16.0,
),
子对象:对齐(
对齐:alignment.topLeft,
子项:DropdownButtonHideUnderline(
孩子:下拉按钮(
是的,
提示:文本(“选择过滤器”),
值:opSelected,//此处指定值
项目:项目
.map((json)=>DropdownMenuItem(
子项:文本(json[“displayName”]),值:json)
.toList(),
onChanged:(json){
manageIntState(json,setState);
},
),
),
),
),
);
}
void manageIntState(映射jsonSelected、状态设置器setState){
设置状态(){
dispName=jsonSelected[“displayName”];
//我在这里设置所选的值
opSelected=dispName;
//做一些手术
id=jsonSelected['id'];
type=jsonSelected['type'];
selectedFilterOption=jsonSelected;
if(jsonSelected.containsKey(“数据”)){
圣殿骑士名单;
List dailogContent=List();
templast=jsonSelected['data'];
圣殿骑士
.map((val)=>{
dailogContent.add(val['displayId']),
})
.toList();
_showReportDialog(dailogContent);
}
});
}
但当我跑步时,我会以错误告终

items==null|| items.isEmpty | | value==null | | itsems.where((DropdownMenuItem item)=>item.value==value).length==1不是真的


让我知道我在代码中做错了什么,所以如果我评论它没有显示所选的下拉值,它会这样给我。

当所选的
下拉按钮的
不是它项的值时,就会发生错误

在您的情况下,您的项目值是
json
,它是一个
映射
,而
下拉按钮
的值是
optelected
,它是一个
字符串

因此,您需要更改
optselected
的类型,如下所示:

Map<String, Object> opSelected;
Map<String, dynamic> opSelected;

原因如下:

Hi,好的,我在manageIntState中为opSelected添加了一个打印,在那里opSelected获得了所选的值,但它不会在下拉列表中更新,您知道我需要更改什么吗?在BuildMain下拉列表中,我正在传递一个硬编码的值。您如何调用BuildMain下拉列表()?像这个createBox(BuildContext context,List val,StateSetter setState){buildMainDropdown(val,setState)}以及如何调用createBox()?我想看看如何传递listinside DropdownMenuItem(child:Text(json[“displayName”]),value:json))。toList(),在这里,我设置映射值,而不是给json['displayName'],然后它就可以正常工作了,但我不会将映射传递给onchanged,即manageIntState()