Flutter DropdownButton 条件不起作用
Flutter DropdownButton conditions not working
我有一个值列表,要从其余 API 解析为带有 JSON 的下拉按钮。值被很好地解析,但我应用了一个条件,即当没有可显示的值列表时,它应该只在下拉列表中显示一个文本 "no equipment"。但由于某种原因,这是行不通的。
我的代码:
List data = List();
Future<String> getSWData() async {
setState(() {
isLoading = true;
});
var res = await http
.get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
resBody = json.decode(res.body);
setState(() {
data = resBody;
isLoading = false;
});
return "Sucess";
}
child: DropdownButton(
hint: new Text(
"Select Equipment",
style: style,
),
style: style,
onChanged: (newVal) {
setState(() {
_mySelectionEquipment = newVal;
});
},
value: _mySelectionEquipment,
items: data.map((item) {
return new DropdownMenuItem(
child: Row(
children: <Widget>[
Text(item['EquipmentMake'] ??
"No Equipment Registered"),
SizedBox(
width: 5,
),
SizedBox(
width: 5,
),
Text(item['EquipmentModel'] ?? ""),
],
),
value: item['EquipmentID'].toString(),
);
}).toList(),
isExpanded: false,
),
您在空列表上调用 Map
尝试这样的事情:
items: data.length > 0
? data.map((item) {
return new DropdownMenuItem(
child: Row(
children: <Widget>[
Text(item['EquipmentMake'] ??
"No Equipment Registered"),
SizedBox(
width: 5,
),
SizedBox(
width: 5,
),
Text(item['EquipmentModel'] ?? ""),
],
),
value: item['EquipmentID'].toString(),
);
}).toList()
: [
DropdownMenuItem(
child: Row(
children: <Widget>[
Text("No Equipment Registered"),
SizedBox(
width: 5,
),
SizedBox(
width: 5,
),
Text(""),
],
),
value: 0.toString())
],
我有一个值列表,要从其余 API 解析为带有 JSON 的下拉按钮。值被很好地解析,但我应用了一个条件,即当没有可显示的值列表时,它应该只在下拉列表中显示一个文本 "no equipment"。但由于某种原因,这是行不通的。
我的代码:
List data = List();
Future<String> getSWData() async {
setState(() {
isLoading = true;
});
var res = await http
.get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
resBody = json.decode(res.body);
setState(() {
data = resBody;
isLoading = false;
});
return "Sucess";
}
child: DropdownButton(
hint: new Text(
"Select Equipment",
style: style,
),
style: style,
onChanged: (newVal) {
setState(() {
_mySelectionEquipment = newVal;
});
},
value: _mySelectionEquipment,
items: data.map((item) {
return new DropdownMenuItem(
child: Row(
children: <Widget>[
Text(item['EquipmentMake'] ??
"No Equipment Registered"),
SizedBox(
width: 5,
),
SizedBox(
width: 5,
),
Text(item['EquipmentModel'] ?? ""),
],
),
value: item['EquipmentID'].toString(),
);
}).toList(),
isExpanded: false,
),
您在空列表上调用 Map
尝试这样的事情:
items: data.length > 0
? data.map((item) {
return new DropdownMenuItem(
child: Row(
children: <Widget>[
Text(item['EquipmentMake'] ??
"No Equipment Registered"),
SizedBox(
width: 5,
),
SizedBox(
width: 5,
),
Text(item['EquipmentModel'] ?? ""),
],
),
value: item['EquipmentID'].toString(),
);
}).toList()
: [
DropdownMenuItem(
child: Row(
children: <Widget>[
Text("No Equipment Registered"),
SizedBox(
width: 5,
),
SizedBox(
width: 5,
),
Text(""),
],
),
value: 0.toString())
],