[英]A nullable expression can't be used as a condition. Try checking that the value isn't 'null' before using it as a condition. flutter /dart
[英]Dropdown list with condition. Flutter/Dart
这是我的下拉列表。 如何更改代码以具有条件,如果用户选择一个选项,它将为“valueChoose_f4”提供选择的选项,否则为“valueChoose_f4”提供存储在“M${widget.orgUnit}”中的值。 谢谢,希望我的问题有意义。
ListTileTheme(
tileColor: Colors.white,
child: ListTile(
title: Container(
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(color: Colors.blueGrey),
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
isExpanded: true,
value: valueChoose_f4,
hint: Text('M${widget.orgUnit}'),
onChanged: (newValue){
setState(() {
valueChoose_f4 = newValue;
});
},
items: listUserType.map((map) {
return DropdownMenuItem<String>(
value: map['value'],
child: Text(map['name']),
);
}).toList(),
),
),
),
),
),
),
您不能提供未包含在 listUserType 中的下拉列表值,因此,如果您想设置下拉列表初始化且尚未选择的默认值,您可以这样做:
value: valueChoose_f4 ?? 'Your_default_value'
这意味着当 valueChoose_f4 为 null 时,默认值将改为 null。
String? valueChoose_f4;
ListTile(
title: Container(
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(color: Colors.blueGrey),
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
isExpanded: true,
value: valueChoose_f4 ?? listUserType.first['value'], // changed
hint: const Text('M${widget.orgUnit}'),
onChanged: (newValue){
setState(() {
valueChoose_f4 = newValue;
});
},
items: listUserType.map((map) {
return DropdownMenuItem<String>(
value: map['value'],
child: Text(map['name']),
);
}).toList(),
),
),
),
),
)
我希望这能解决你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.