问题
If someone can please help me with this issue. So I am able to fetch and insert into the dropdownbuttons using the code below. If i select the first dropdown then the list shows accordingly for the second dropdown and if i pick another value the list changes. But when I select between the lists more than once it gives me an error for example:
Another exception was thrown: There should be exactly one item with [DropdownButton]'s value: Ointments.
Either zero or 2 or more [DropdownButton]'s were detected with the same value.
I assume this is because I'm calling the functions inside the onChanged: of the buttons. Any help would be much appreciated. Thanks!
note: I have also put only the dynamicDropDownMainCategory() function in initState.
//FIRST DROPDOWNBUTTON BELOW
//
child: DropdownButton<String>(
hint: Text('Select Category'),
icon: Icon(Icons.keyboard_arrow_down),
iconSize: 28,
isDense: true,
isExpanded: true ,
elevation: 16,
style: TextStyle(
color: Colors.black,
fontSize: 16.0,
),
value: categoryManualCurrent.isNotEmpty ? categoryManualCurrent : null,
onChanged: (String categoryValue) async {
// await dynamicDropDownMainCategory();
setState(() {
mainCategoryCurrent = categoryValue;
categoryManualCurrent = categoryValue;
});
print('THIS' + categoryManual.toString());
print('THAT' + subCategoryManual.toString());
await dynamicDropDownSubCategory();
},
items: categoryManual.toList()
.map((var value3) {
return DropdownMenuItem<String>(
value: value3,
child: Text(value3),
);
}).toList(),
),
)
],
),
//SECOND DROPDOWNBUTTON BELOW
//
child: DropdownButton<String>(
hint: Text('Select subCategory'),
icon: Icon(Icons.keyboard_arrow_down),
iconSize: 28,
isDense: true,
isExpanded: true ,
elevation: 16,
style: TextStyle(
color: Colors.black,
fontSize: 16.0,
),
value: subCategoryManualCurrent.isNotEmpty ? subCategoryManualCurrent : null,
onChanged: (String subCategoryValue) async {
//await dynamicDropDownMainCategory();
await dynamicDropDownSubCategory();
subCategoryCurrent = subCategoryValue;
setState(() {
subCategoryManualCurrent = subCategoryValue;
});
// await dynamicDropDownSubCategory();
},
items: subCategoryManual.toList()
.map((var value1) {
return DropdownMenuItem<String>(
value: value1,
child: Text(value1),
);
}).toList(),
),
),
]
),
//TWO functions im using to call the list data from firestore and insert into the //dropdownbutton
Future dynamicDropDownMainCategory() async{
await Firestore.instance
.collection('Products')
.document('Categories')
.get()
.then((snapshot) => {
categoryManual = (snapshot.data['mainCategory']),
}
);
}
Future dynamicDropDownSubCategory() async{
await Firestore.instance
.collection('Products')
.document('Categories')
.get()
.then((snapshot) => {
if (mainCategoryCurrent == categoryManualCurrent){
subCategoryManual = (snapshot.data['subCategory'+' '+mainCategoryCurrent]),
} else {
subCategoryManual = ['Error Getting Data'],
}
}
);
}
回答1:
this error happens when u have two strings that are equal & u set it as value to dropdowmbutton as it need to be unique :
return DropdownMenuItem(
value: data.categoryName,
child: Text(data.categoryName, style: Constants.normalLarge),
);
in above example the value property need to be unique so that it can render appropriate item.
来源:https://stackoverflow.com/questions/61068629/flutter-dependent-multilevel-dropdownbutton-has-an-issue-there-should-be-exac