在我的代码中添加了一个下拉列表,如下所示:当我在下拉列表中切换选择时,其未更新其显示异常,我在statefull小部件中声明了一个变量,在下拉函数中,我将其分配为A值到下拉按钮,在Onchanged中,我将json传递给另一个函数,我从变量中获取值并将其分配给setState内的opSelected变量
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);
}
});
}
但是,当我跑步时,我将出现错误
items == null || items.isEmpty || value == null || itsems.where(((DropdownMenuItem item)=> item.value == value).length == 1不正确..
让我知道我在代码中做错了什么,如果我评论它未显示所选的下拉值,它就会给我这样的提示。
答案 0 :(得分:0)
当value
中选定的DropdownButton
不是它的项目值之一时,就会发生该错误。
在您的情况下,您的项目值为json
,即Map<String, Object>
,而DropdownButton
的值为opSelected
,即String
。
因此,您需要像这样更改opSelected
的类型:
Map<String, Object> opSelected;
还要确保将相同的项目列表引用传递给buildMainDropdown()
,因为如果在调用buildMainDropdown()
时创建新列表,则DropdownButton
将具有另一个引用的选项,它是不允许的
注意:,您可能希望对地图使用动态而不是对象,就像这样:
Map<String, dynamic> opSelected;
这是原因:What is the difference between dynamic and Object in dart?