Flutter - 应该只有一项具有 [DropdownButton] 的值
Flutter - There should be exactly one item with [DropdownButton]'s value
请有人帮助我。我在 StatefullWidget
上创建了带有映射键和值的 DropdownButton
我在这里使用地图是因为我正在将选定的键值发送到父窗口小部件。
我收到错误:
应该只有一项具有 [DropdownButton] 的值:Все подряд。
检测到具有相同值的零个或两个或更多 [DropdownMenuItem]
我一删除 DropdownButton 小部件中的值,就不会抛出任何错误
title: Container(
child: DropdownButtonHideUnderline(
child: DropdownButton(
**value: _options[dropDownValue]**,
But in this case there is no value for the dropdown button
完整代码:
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
。
请有人帮助我。我在 StatefullWidget
上创建了带有映射键和值的 DropdownButton我在这里使用地图是因为我正在将选定的键值发送到父窗口小部件。 我收到错误: 应该只有一项具有 [DropdownButton] 的值:Все подряд。 检测到具有相同值的零个或两个或更多 [DropdownMenuItem]
我一删除 DropdownButton 小部件中的值,就不会抛出任何错误
title: Container(
child: DropdownButtonHideUnderline(
child: DropdownButton(
**value: _options[dropDownValue]**,
But in this case there is no value for the dropdown button
完整代码:
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
。