I have listview that contain this dropdown. Items of the drop are shown, it’s from API, but it throw some error when i select the value
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
‘package:flutter/src/material/dropdown.dart’:
Failed assertion: line 888 pos 15: ‘items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem item) {
return item.value == value;
}).length == 1’"
I guess it’s throw that error because of duplicate dropdown in the lisview that use same item.I think it’s should use index for different the dropdown. Any suggestions code examples please!
List? answerList;
This is function to fetch data:
void answerSetupDropdown() async {
var headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
};
var request =
http.Request('POST', Uri.parse(baseLaravelAPI + '/api/start_section'));
request.body = json.encode({
"staffid": widget.staffId.toString(),
"form_open_id": widget.formOpenId.toString(),
"section_id": widget.sectionId.toString(),
"form_id": widget.formId.toString(),
"confirm_no": widget.confNo.toString()
});
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
final res = jsonDecode(await response.stream.bytesToString());
answerList = res["data"]["answer_list"];
loading = false;
} else {
throw Exception('Unable to fetch products from the REST API');
}
}
This is dropdown that show the item:
ListView.builder(
itemCount: chilList!.length,
itemBuilder: (context, i) {
loading == false
? Expanded(
// alignment: Alignment.centerLeft,
child: Theme(
data: Theme.of(context).copyWith(
unselectedWidgetColor: lightblue,
),
child: DropdownButton<dynamic>(
underline: SizedBox(),
hint: Text("Select Answer"),
value: selectedValue,
onChanged: (dynamic value) {
setState(() {
selectedValue =
(value["answer_name"])
.toString();
});
// selectedValue =
// (valueSel["answer_name"])
// .toString();
},
items: answerList!.map((answer) {
return DropdownMenuItem<dynamic>(
value: answer,
child: Row(
children: <Widget>[
SizedBox(
width: 10,
),
Text(
answer["answer_name"]
.toString(),
style: TextStyle(
color: Color(
int.parse((answer[
"answer_color"])
.replaceAll(
RegExp(
'#'),
'0xFF')))),
),
],
),
);
}).toList(),
),
),
)
: CircularProgressIndicator(),
});
2
Answers
Ensure that the
selectedValue
is unique for each itemThat means your
answerList
variable has 2 identical values. BecauseDropdownMenuItem
value must be unique.In example:
Bad
Good
Code Example