![](/img/trans.png)
[英]Flutter GetBuilder Dependent DropDownButton - There should be exactly one item with [DropdownButton]'s value even when value has been reset
[英]Flutter - Dependent/Multilevel DropdownButton has an issue: There should be exactly one item with [DropdownButton]'s value: Ointments
如果有人可以幫助我解決這個問題。 所以我可以使用下面的代碼獲取並插入下拉按鈕。 如果我 select 是第一個下拉列表,則列表會相應地顯示第二個下拉列表,如果我選擇另一個值,列表會更改。 但是,當我在列表之間多次 select 時,它給了我一個錯誤,例如:
引發了另一個異常:應該只有一個具有 [DropdownButton] 值的項目:Ointments。
檢測到具有相同值的 0 個或 2 個或多個 [DropdownButton]。
我認為這是因為我正在調用按鈕的 onChanged: 內的函數。 任何幫助將非常感激。 謝謝!
注意:我也只將 dynamicDropDownMainCategory() function 放在 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'],
}
}
);
}
當您有兩個相等的字符串並且您將其設置為 dropdowmbutton 的值時,會發生此錯誤,因為它需要是唯一的:
return DropdownMenuItem(
value: data.categoryName,
child: Text(data.categoryName, style: Constants.normalLarge),
);
在上面的示例中, value 屬性需要是唯一的,以便它可以呈現適當的項目。
這是因為依賴列表具有不同的大小,所以基本上父 1 有一個包含 4 個元素的依賴下拉列表,你 select 最后一個 [索引 3]。 然后您更改為具有 3 個元素的依賴 dd 的父 2,發生的情況是依賴下拉列表仍然具有索引 3,因為您僅更改了父級,並且由於父級 2 依賴 dd 僅轉到索引 2,因此會出現此錯誤.
希望我有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.