Reputation: 53
Please someone help me. I created DropdownButton with map key and values on StatefullWidget
I am using a map here because I am sending the selected key of value to the parent widget. I am getting error: There should be exactly one item with [DropdownButton]'s value: Все подряд. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
As soon as I remove the value in the DropdownButton Widget, no error is thrown
title: Container(
child: DropdownButtonHideUnderline(
child: DropdownButton(
**value: _options[dropDownValue]**,
But in this case there is no value for the dropdown button
Full Code:
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);
},
)
],
);
}
}
Upvotes: 0
Views: 446
Reputation: 1530
change your DropdownButton
value property to just dropDownValue
only (not _options[dropDownValue]
).
Because in DropdownMenuItem
you are setting value
as the key
of the MapEntry
.
Upvotes: 1