发生异常。_AssertionError ('package:flutter/src/material/dropdown.dart':失败的断言:第 882 行 pos 15: 'items =


Exception has occurred.
_AssertionError ('package:flutter/src/material/dropdown.dart': Failed assertion: line 882 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1': There should be exactly one item with [DropdownButton]'s value: 1. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value)

我有这个错误,而我试图发布1,如果选择的下拉菜单是"全职",2,如果选择的下拉菜单是"兼职",3,如果选择的下拉菜单是"实习">

DropdownButton(
hint: Text("Select Job type"),
value: currentValue,
isDense: true,
icon: const Icon(Icons.keyboard_arrow_down),
items: <String>[
if (currentValue == "1")
("Full-time")
else if (currentValue == "2")
("Part-time")
else
"Internship",
].map((String i) {
return DropdownMenuItem(
value: i,
child: Text(i),
);
}).toList(),
onChanged: onChangedCallback,
),

我有onChangedCallback如下

var currentValue = "1";

Future<void> onChangedCallback(String? item) async {
if (item != null) {
currentValue = item;
final url = "my-api-link";
final body = {"job_type": item};
final response = await http.post(Uri.parse(url), body: body);
print(response.body);
}
}

试试这个:

String? currentValue;
var _dropdownItems = <String>['Full-time', "Part-time", "Internship"];
// put the following inside initState() or build()
// start
_dropdownMenuItems = _dropdownItems
.map((String value) =>
DropdownMenuItem(child: Text(value), value: value))
.toList();
// end
DropdownButton(
hint: Text("Select Job type"),
value: currentValue,
isDense: true,
icon: const Icon(Icons.keyboard_arrow_down),
items: _dropdownMenuItems,
onChanged: onChangedCallback,
);
/* ... */
Future<void> onChangedCallback(String? item) async {
if (item != null) {
// change this:
// setState(() => currentValue = item);
// to: 
setState(() => currentValue = _dropdownItems.indexOf(item!).toString());
//
final url = "my-api-link";
final body = {"job_type": item};
final response = await http.post(Uri.parse(url), body: body);
print(response.body);
}
}

在填充items参数之前,必须先设置value参数

参考下面的例子,其中'dropdownValue'初始化为'list '。首先,我得到了异常,因为这个。

<>之前导入的包:颤振/material.dart ';///[DropdownButton]的颤振代码示例。常量列表列表=['一',' 2 ',' 3 ',' 4 '];void main() => runApp(const DropdownButtonApp());类DropdownButtonApp扩展StatelessWidget {const DropdownButtonApp ({super.key});@override小部件构建(BuildContext) {返回MaterialApp (家:支架(appBar: appBar (title: const Text('DropdownButton Sample')),body: const Center()孩子:DropdownButtonExample (),),),(;}}类DropdownButtonExample扩展StatefulWidget {const DropdownButtonExample ({super.key});@overrideState createState() => _DropdownButtonExampleState();}类_DropdownButtonExampleState扩展状态{字符串dropdownValue = list.first;@override小部件构建(BuildContext) {返回DropdownButton (价值:dropdownValue,icon: const icon (icons . arrow_向下),海拔:16,style: const TextStyle(color: colors . deepurple);强调:容器(高度:2颜色:Colors.deepPurpleAccent,),onchange:(字符串?值){//当用户选择一个项目时调用。设置状态((){dropdownValue = value!;}(;},项目:列表。map>((字符串值){返回DropdownMenuItem (价值:价值,孩子:文本(值),(;}) .toList (),(;}}