[英]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.