I made DropdownButton with Flutter, and I've got the error "There should be exactly one item with [DropdownButton]'s value: A. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value 'package:flutter/src/material/dropdown.dart': Failed assertion: line 890 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) { return item.value == value; }).length == 1'"
here is the code
class HomeController extends GetxController {
List<String> sections = [
'A',' B',' C', ' D',' E',' F',' G'];
String selectedLetter = "A";
void setSectionLetter(String s) {
selectedLetter = s;
update();
}
}
GetBuilder<HomeController>(builder: (contH) {
return DropdownButton<String?>(
items: contH.sections
.map((e) => DropdownMenuItem<String?>(
child: HDW().title(context: context, text: e)))
.toList(),
value: contH.selectedLetter.isNotEmpty
? contH.selectedLetter
: null,
onChanged: (value) {
contH.setSectionLetter(value!);
},
isExpanded: true,
icon: const Icon(Icons.keyboard_arrow_down),
);
})
If
contH.selectedLetter.isNotEmpty
is
false
, the
value
is set to
null
.
However,
sections
does not contain the option
null
.
This explains the error: zero [DropdownMenuItem]s were detected with the value
null
.
Either add
null
to the sections or change
null
to a value that is already in
sections
.
EDIT: I tried to make a DartPad sample for this and found that the actual problem is that you're not passing a value to your DropdownMenuItems. Aside from a child that is used to represent a value, there is also a value
parameter that stores the actual value of that option. See line 44 in this DartPad: https://dartpad.dev/?id=57eb35e737ca1420aca6a833b8bb716f
You can use the exact same line in your code I would say.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.