Home > front end >  How can I solve "There should be exactly one item with [DropdownButton]'s value: null.&quo
How can I solve "There should be exactly one item with [DropdownButton]'s value: null.&quo

Time:10-05

I want to get some details from database and put that values to dropdown menu. I have successfully got the information that I want to put on dropdown from database and put it into dropdown list.

But when I tried to select one item from dropdown list, it shows this error.

There should be exactly one item with [DropdownButton]'s value: null. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value

Here is the code I wrote so far.

Variables

var supplier;
var _supplierDetails = [];

Init Method

@override
void initState() {
  super.initState();
  getSupliers();
}

Get Suppliers

void getSupliers() async {
final responseSup =
    await http.get(Uri.parse('http://170.14.0.0:8020/supplier/'));
final jsonDataSup = jsonDecode(responseSup.body) as List;
print(jsonDataSup);

setState(() {
  _supplierDetails = jsonDataSup;
});
}

DropDown menu implemented

DropdownButton(
     hint: Text('Select Supplier'),
     items: _supplierDetails.map((list) {
         return DropdownMenuItem(
             child: Text(list['Name']),
                value: list['id'].toString(),
              );
            }).toList(),
            onChanged: (value) {
              setState(() {
                supplier = value;
              });
            },
            value: supplier,
)

Can someone help me to fix this issue please? Thank you

CodePudding user response:

It seems there are more than one item where item['id'] is null. DropdownButton need all it's items to have a unique value

  • Related