I have created the DropdownBotton inside the ListView and i have a list of values which needs to be displayed in the DropdownMenuItem.
I have 2 questions with respect to DropdownBotton,
- How to set the default value in the DropdownButton for my scenario.
- (console error)
There should be exactly one item with [DropdownButton]'s value: $ 5.50 Pineapple Jack (S).
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 915 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
Below is my code:
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: const ScrollPhysics(),
itemCount: snapshot.data!.length,
itemBuilder: (_, index) {
List<String> selectedItemValue = <String>[
("\$${snapshot.data![index].price} ${snapshot.data![index].productType.type1}"),
("\$${snapshot.data![index].price} ${snapshot.data![index].productType.type2}"),
("\$${snapshot.data![index].price} ${snapshot.data![index].productType.type3}")];
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.symmetric(
horizontal: 10, vertical: 5),
padding: const EdgeInsets.all(5.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: dropdownValue,
items: selectedItemValue.map((dropValue) {
return DropdownMenuItem<String>(
value: dropValue,
child: Text(dropValue),
);
}).toList(),
onChanged:
(newDropdownValue) {
setState(() {
dropdownValue =
newDropdownValue!;
print(
'dropdown: $dropdownValue');
});
},
),
),
),
],
);
}
),
Please someone help me on this issue, Thanks in advance.