Flutter-下拉值 [英] Flutter - Dropdown value
问题描述
我需要在flutter下拉列表中使用字符串作为值。但是返回
I need use a string as a value in the flutter dropdown. But returns
引发了另一个异常:
'package:flutter / src / material / dropdown.dart':失败的断言:行
609 pos 15:'items == null || items.isEmpty ||值== null ||
items.where(((DropdownMenuItem item)=> item.value == value).length
== 1':不正确。
Another exception was thrown: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 609 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true.
完整日志此处。
代码为
items: dataMaker.map((item) {
return new DropdownMenuItem<String>(
child: new Text(item['fabricante'],
textAlign: TextAlign.center),
value: item['fabricante'], //FAIL
);
}).toList(),
所以我的问题在:如何使用字符串作为项目值?另一个解决方案是获取下拉菜单中的文本,但我不知道该怎么做。
So my question in: How can i use a string as item value? Another solution is get the text of dropdown but i don´t know how.
-解决方案-
使用此代码,我可以在dataMaker列表中找到与下拉菜单具有相同文本的所有元素。
With this code I can find all the elements in the dataMaker list that have the same text as the drop-down menu.
var test =dataMaker.firstWhere((fabricante) => fabricante["id"].toString() == dropdownSelectionMaker);
dataModelo = dataMaker.where((modelo) => modelo["fabricante"] == test["fabricante"]).toList();
推荐答案
我不确定库如何进行比较但我发现尽管该值在他的列表中,但仍返回错误,因此我不得不进行一些手动比较。注意: BuildingType
只是一个自定义数据类型。
这是我的案例的神奇之处:
value:selectedType == null吗? selectedType:buildingTypes.where((i)=> i.name == selectedType.name)。首先作为BuildingType,
I'm not sure how the library does the comparison but I found that though the value was in he list it still returned the error, so I had to do some manual comparison. NOTE: BuildingType
is just a custom data type.
Here's the magical line for my case:
value: selectedType == null ? selectedType : buildingTypes.where( (i) => i.name == selectedType.name).first as BuildingType,
return DropdownButton<T>(
hint: Text("Select Building type"),
value: selectedType == null ? selectedType : buildingTypes.where( (i) => i.name == selectedType.name).first as BuildingType,
onChanged: handleBuildingTypeChange,
items: abcList.map((T value) {
return DropdownMenuItem<T>(
value: value,
child: Row(
children: <Widget>[
SizedBox(
width: 10,
),
Text(
value.name,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList(),
);
这篇关于Flutter-下拉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!