I am trying to assign the default value to the DropdownButtonFormField in a flutter. I am trying to edit the record but I am not able to assign the default value to it.
I have workersList as a List
.
DropdownButtonFormField<String>(
value: workersList[0]['worker_name'],
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: "Payment To",
prefixIcon: Icon(
Icons.phone_android,
color: Colors.blue,
),
),
validator: (value) {
if (value == null) {
return "Please Select Worker";
}
return null;
},
hint: const Text('Select Worker'),
items: workersList.map((Map value) {
return DropdownMenuItem<String>(
value: value['worker_id'].toString(),
child: Text(value['worker_name']),
);
}).toList(),
onChanged: (value) {
_workerController.text = value.toString();
},
),
CodePudding user response:
Since you set the value
property of the DropDownMenuItem
to ['worker_id']
, you need to set the initial value like:
DropdownButtonFormField<String>(
value: workersList[0]['worker_id'],
...
)
CodePudding user response:
You have to use the same property that you used for DropDownMenuItem as a value, So just change the value
DropdownButtonFormField<String>(
value: workersList[0]['worker_name'],
...
)
to
DropdownButtonFormField<String>(
value: workersList[0]['worker_id'].toString(),
...
)