Currently I'm trying to implement dropdown using DropdownButton
, but there is one problem.
In DropdownButton()
, if value:
is empty, error is not occurred but nothing change when choose.
if I assign an arbitrary String variable to value:
, the following error will occur.
════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building StatefulBuilder(dirty, state: _StatefulBuilderState#65c7c):
Assertion failed:
..\…\material\dropdown.dart:915
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: . \nEither zero or 2 or more [DropdownMenuItem]s were detected with the same value"
My code;
List<String> strings = <String>["Aa", "Bb", "Cc"];
String selectedString = "";
@override
Widget build(BuildContext c) { ....
showDialog( builder: (context) {
return Dialog(builder: (context, setState) {
return SingleChildScrollView(
child: Column( ....
DropdownButton<String>(
value: selectedString, // IF THIS IS NOT NULL, THE ERROR NOT APPEARD
hint: Text("Choose"),
items: strings.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedString = value!;
});
},
),
........
CodePudding user response:
You have to follow these three steps;
1. List<String> strings = <String>[" ","Aa", "Bb", "Cc"];
2. String selectedString = " ";
3. value: selectedString ,
CodePudding user response:
This was easy to solve.
Flutter: There should be exactly one item with [DropdownButton]'s value
This answer says that selectedString
value should be string that exists in strings
. Like this
selectedString = "Aa";