问题描述
如果有人可以帮助我解决这个问题.因此,我可以使用以下代码获取并插入到dropdownbuttons中.如果我选择第一个下拉列表,则列表将为第二个下拉列表显示相应的内容;如果我选择另一个值,则列表将更改.但是,当我在列表中多次选择时,会给我一个错误,例如:
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:
引发了另一个异常:应该只有[DropdownButton]值的一项是软膏.
检测到零个或两个或两个以上具有相同值的[DropdownButton].
我认为这是因为我正在调用按钮的onChanged:内部的函数.任何帮助将非常感激.谢谢!
I assume this is because I'm calling the functions inside the onChanged: of the buttons. Any help would be much appreciated. Thanks!
注意:我还只将dynamicDropDownMainCategory()函数放在initState中.
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'],
}
}
);
}
推荐答案
当您有两个等于&的字符串时,会发生此错误.您需要将其设置为dropdowmbutton的值,因为它必须是唯一的:
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),
);
在上面的示例中,value属性必须是唯一的,以便它可以呈现适当的项目.
in above example the value property need to be unique so that it can render appropriate item.
这篇关于Flutter-依赖/多级DropdownButton存在问题:[DropdownButton]的值应该恰好是一项:软膏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!