【发布时间】:2023-01-05 19:41:37
【问题描述】:
我正在尝试在我的 flutter 应用程序中使用下拉菜单,但出现错误。
这是代码:
List<String> items = ["Item1", "Item2", "Item3", "Item4"];
String selectedItem = "Item1";
DropdownButton<String>(
items: items.map(
(txt) {
return DropdownMenuItem<String>(
child: Text(
"$txt"
),
);
}
).toList(),
value: selectedItem,
)
在某些问题中,我看到我们必须首先将变量设置为列表中存在的值。我已经完全做到了,但仍然出现错误。
错误信息:
There should be exactly one item with [DropdownButton]'s value: Item1.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 850 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
这里的错误是什么?
如果需要更多信息,请发表评论。
【问题讨论】: