我有一个可以正常工作的下拉按钮,但是当我尝试设置默认值时,它将失败并出现以下错误:
'package:flutter/src/material/dropdown.dart':断言失败:第 620 行 pos 15:'items == null || 项目.isEmpty || 值 == 空 || items.where((DropdownMenuItem item) => item.value == value).length == 1': 不正确。
这是我的下拉按钮:
Widget changeWorkspace() {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: DropdownButton<AssignedWorkspace>(
isExpanded: true,
hint: Text("SELECT WORKSPACE"),
value: selectedWorkspace,
onChanged: (dropdownValueSelected) {
setState(() {
selectedWorkspace = dropdownValueSelected;
});
},
items: workspaces != null && workspaces.length > 0
? workspaces.map((AssignedWorkspace workspace) {
return new DropdownMenuItem<AssignedWorkspace>(
value: workspace,
child: new Text(workspace.name,
style: new TextStyle(color: Colors.black)),
);
}).toList()
: null),
),
]);
});
}
我尝试如下设置 selectedWorkspace onInit 的值,但失败了。
selectedWorkspace = new AssignedWorkspace(
id: userSettings.currentWorkspaceId,
name: userSettings.currentWorkspaceName);
有没有办法在下拉按钮中设置默认值?