Home > Net >  There should be exactly one item with [DropdownButton]'s value: Select district. Either zero or
There should be exactly one item with [DropdownButton]'s value: Select district. Either zero or

Time:02-28

I keep getting a dropdown errors, There should be exactly one item with [DropdownButton]'s value: Select district. Either zero or 2 or more [DropdownMenuItem]s were detected.

If I change something get another one. API is working fine and data is added to the list. everything works but this one didn't.

CodePudding user response:

For your each DropdownMenuItem<String> you have to also provide value: argument, this argument has to be different on each DropdownMenuItem and one of them has to be equal to DropdownButtonFormField<String> value:

As an example:

final List<DropdownMenuItem<String>> ll = [
  DropdownMenuItem<String>(
    value: '1',
    child: Text('1'),
  ),
  DropdownMenuItem<String>(
    value: '2',
    child: Text('2'),
  ),
];

@override
Widget build(BuildContext context) {
  return DropdownButtonFormField<String>(
    value: '1',
    items: ll,
    onChanged: (value) => print(value),
  );
}
  • Related