我有这个 DropdownButton
它的列表中有两个项目
当我选择值 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
我希望有人能帮帮忙 !:)
[为了搜索引擎优化,这里是错误]:
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"