body: Center(
child: DropdownButton(
items: ["bad ", "good ", "ok", "sad", "happy"]
.map((e) => DropdownMenuItem(
child: Text("$e"),
value: e,
))
.toList(),
onChanged: (val){},
value: selectedfeeling,
),
),
This is the error:
Exception has occurred.
_AssertionError ('package:flutter/src/material/dropdown.dart': Failed assertion: line 882 pos 15: '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: good.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value)
CodePudding user response:
It is because, perhaps you are not giving initial value to selectedfeeling
, which is exist in
["bad ", "good ", "ok", "sad", "happy"]
Before calling build method, for example in initState or when declaring selectedfeeling
just give value to selectedfeeling
, which exist here
["bad ", "good ", "ok", "sad", "happy"]
CodePudding user response:
@override
void initState() {
selectedfeeling="bad";
}
body: Center(
child: DropdownButton(
items: ["bad ", "good ", "ok", "sad", "happy"]
.map((e) => DropdownMenuItem(
child: Text("$e"),
value: e,
))
.toList(),
onChanged: (val) {
selectedfeeling=val;
},
value: selectedfeeling,
),
),
CodePudding user response:
You can create nullabe string like selected value like
String? selectedfeeling;
Now to provide initial value, you must have to provide a value that exists on items
final items = ["bad ", "good ", "ok", "sad", "happy"];
late String? selectedfeeling = items[1]; //default is good
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: DropdownButton(
items: items
.map((e) => DropdownMenuItem(
child: Text("$e"),
value: e,
))
.toList(),
onChanged: (val) {},
value: selectedfeeling,
),
),
);
}