YA TUBE
YA TUBE

Reputation: 15

There should be exactly one item with [DropdownButton]'s value: Instance of Categorie

hi I work to update value from DropdownButton.but I get this error:

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

                     padding: EdgeInsets.only(top: 0.0, bottom: 10.0),
                     child: new DropdownButton<Categorie>(
                             hint: new Text("Selectionnez une catégorie"),
                             value: selectedCataId,
                             onChanged: (Categorie newValue) {
                               setState(() {
                                 selectedCataId = newValue;
                                 pAudit.categorie = selectedCataId;
                                 debugPrint('C selectedCataId: ' + selectedCataId.toString());
                                 debugPrint('C pAudit.categorieId: ' + pAudit.categorie.id.toString());
                               });
                             },
                             items: this.cataList.map((Categorie dropDownCataItem) {
                               return new DropdownMenuItem<Categorie>(
                                 value: dropDownCataItem,
                                 child: new Text(
                                  /* dropDownCataItem.abreviation*/  ' - ' + dropDownCataItem.label,
                                   style: new TextStyle(color: Colors.black),
                                 ),
                               );
                             }).toList(),
                     ),
                   ),

Upvotes: 1

Views: 87

Answers (1)

Abbasihsn
Abbasihsn

Reputation: 2171

DropdownButton needs a list of items with unique key for each of them as the value. then DropdownButton would use these values (key) to distinguish different items. it seems that your cataList has one or more duplicate items. if so, you would have two solutions:

  1. add a unique key parameter to each item in cataList and use it as a value, therefore you can have the same items in your dropdown (if you need so).
  2. remove one the duplicate items!

Upvotes: 1

Related Questions