【发布时间】:2021-09-20 21:13:08
【问题描述】:
我有这个下拉按钮
列表中有两个项目
当我选择值 2 时,我希望它显示列表中未包含的值
像这样:
有可能吗?
看起来像这样:
DropdownButton<String>(
value: dropdownValue,
items: <String>[
'Value 1',
'Value 2',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
if (dropdownValue == 'Value 2') {
dropdownValue = 'Value Not in list of items';
}
});
},
),
如您所见,如果我设置 dropdownValue = 'Value Not in list of items';,我当然会收到错误,因为它不在列表中 - 但我希望它能够工作 :)
另一方面,正如预期的那样,这是可行的:
if (dropdownValue == 'Value 2') {
dropdownValue = 'Value 1';
}
因为'Value 1' 在列表中 - 但我想在单击Value 2 时显示不在列表中的值
希望有人能帮忙! :)
[为了 SEO,这里是错误]:
Assertion failed:
../…/material/dropdown.dart:880
items == null ||
items.isEmpty ||
value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length ==
1
"There should be exactly one item with [DropdownButton]'s value: Value Not in list of items. \nEither zero or 2 or more [DropdownMenuItem]s were detected with the same value"
【问题讨论】:
标签: flutter dropdownbutton flutter-dropdownbutton