Reputation: 316
I try to set value of dropdown button in Flutter. Actually, it takes item values from a List.
When I add value: _cStatus,
in Dropdownbutton, I received the error message below:
There should be exactly one item with [DropdownButton]'s value: . Either zero or 2 or more [DropdownMenuItem]s were detected with the same value 'package:flutter/src/material/dropdown.dart': Failed assertion: line 1543 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) { return item.value == value; }).length == 1'
class _CheckinFormState extends State<CheckinForm> {
final List<String> cStatusList = ['Checkin', 'CheckOut'];
// form values
String _cStatus='Checkin';
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: <Widget>[
Text(
'Update Your Checkin Status.',
style: TextStyle(fontSize: 18.0),
),
SizedBox(height: 20.0),
DropdownButtonFormField(
value: _cStatus, //my problem is here
items: cStatusList.map((cstatus) {
return DropdownMenuItem(
value: cstatus,
child: Text('$cstatus'),
);
}).toList(),
onChanged: (val) {
setState(() {
_cStatus=val.toString();
});
},
),
SizedBox(height: 10.0),
ElevatedButton(
child: Text(
'Update',
style: TextStyle(color: Colors.white),
),
onPressed: () async {
...
}),
],
),
);
}
}
Upvotes: 1
Views: 4235
Reputation: 316
After I added String type to DropdownButtonFormField
as DropdownButtonFormField<String>
, problem is solved.
DropdownButtonFormField<String>(
value: _cStatus,
decoration: textInputDecoration,
items: cStatusList.map((cstatus) {
return DropdownMenuItem(
value: cstatus,
child: Text('$cstatus'),
);
}).toList(),
onChanged: (val) {
setState(() {
_cStatus = val.toString();
});
},
),
Upvotes: 1