I am passing a List of records to a custom DropDown but got the captioned error.
//dataList is a list of record with 2 fields: int id, and String name
Widget DropDownList(dataList, val, ReturnSelectedValue) {
return DropdownButton(
value: val,
isExpanded: true,
items: dataList.map((item) {
return DropdownMenuItem(
value: item.id,
child: Text(item.name!),
);
}).toList(),
onChanged: (selectedvalue) {
ReturnSelectedValue(selectedvalue);
},
);
}
2
Answers
You can use
type inference
to ensure that dataList is of the correct type by using theList<DropdownMenuItem<T>>.
from constructor.here is the full example:
Code: