![](/img/trans.png)
[英]Flutter: There should be exactly one item with [DropdownButton]'s value: A
[英]Flutter - There should be exactly one item with [DropdownButton]'s value
請有人幫助我。 我在 StatefullWidget 上使用 map 鍵和值創建了 DropdownButton
我在這里使用 map,因為我正在將選定的值鍵發送到父小部件。 我收到錯誤消息:應該只有一項具有 [DropdownButton] 的值:Все подряд。 檢測到 0 個或 2 個或多個 [DropdownMenuItem] 具有相同的值
只要我刪除 DropdownButton 小部件中的值,就不會引發錯誤
title: Container(
child: DropdownButtonHideUnderline(
child: DropdownButton(
**value: _options[dropDownValue]**,
完整代碼:
class _MyAppBarState extends State<MyAppBar> {
String dropDownValue = 'created';
Map<String, String> _options = {
'rating': 'Лучшие',
'seen': 'Интересные',
'created': 'Все подряд',
};
@override
Widget build(BuildContext context) {
return AppBar(
backgroundColor: Color(0xff62858F),
elevation: 20,
shadowColor: Colors.grey,
title: Container(
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: _options[dropDownValue],
icon: const Icon(
Icons.arrow_drop_down,
color: Colors.white,
size: 25,
),
iconSize: 24,
elevation: 2,
selectedItemBuilder: (BuildContext context) {
return _options
.map((String key, String value) {
print(key);
print(value);
return MapEntry(
key,
Padding(
padding: EdgeInsets.symmetric(vertical: 15),
child: Text(
value,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 17,
),
),
),
);
})
.values
.toList();
},
//underline:,
items: _options
.map(
(String key, String value) {
return MapEntry(
key,
DropdownMenuItem<String>(
value: key,
child: Text(
value,
style: TextStyle(
color: Colors.black,
),
),
),
);
},
)
.values
.toList(),
onChanged: (String newValue) {
widget.sortBy(newValue);
setState(() {
dropDownValue = newValue;
});
},
),
),
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.search,
size: 25,
),
onPressed: () {
Navigator.of(context).pushNamed(SearchPage.tag);
},
)
],
);
}
}
將您的DropdownButton
值屬性更改為僅dropDownValue
(不是_options[dropDownValue]
)。
因為在DropdownMenuItem
中,您將value
設置為MapEntry
的key
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.