android - 颤动中的下拉按钮异常
问题描述
我正在使用Flutter
和Dropdown
按钮创建一个移动应用程序。发生下拉按钮异常。
'package:flutter/src/material/dropdown.dart': Failed assertion: line 1206 pos 12: 'widget.items!.where((DropdownMenuItem<T> item) => item.value == widget.value).length == 1': is not true.
我正在分享代码快照。
DropdownButton(
items: [
DropdownMenuItem(
child: Row(
children: [
CircleAvatar(
backgroundColor: Colors.green,
radius: 10,
),
SizedBox(
width: 4,
),
Text(
"Green",
style: TextStyle(fontWeight: FontWeight.w600),
),
],
),
),
DropdownMenuItem(
child: Row(
children: [
CircleAvatar(
backgroundColor: Colors.blue,
radius: 10,
),
SizedBox(
width: 4,
),
Text(
"Blue",
style: TextStyle(fontWeight: FontWeight.w600),
),
],
),
),
DropdownMenuItem(
child: Row(
children: [
CircleAvatar(
backgroundColor: Colors.black,
radius: 10,
),
SizedBox(
width: 4,
),
Text(
"Black",
style: TextStyle(fontWeight: FontWeight.w600),
),
],
),
),
DropdownMenuItem(
child: Row(
children: [
CircleAvatar(
backgroundColor: Colors.red,
radius: 10,
),
SizedBox(
width: 4,
),
Text(
"Red",
style: TextStyle(fontWeight: FontWeight.w600),
),
],
),
),
],
onChanged: (val) {},
)
有没有办法解决这个问题?期待很快可以收到你的来信。
解决方案
两者DropdownButton
和DropdownMenuItem
具有value
参数都是将显示为选定值的参数。DropdownMenuItem
列表中应该只有一个value
(item
不超过或少于一个)。这就是颤振所抱怨的。您忘记将该value
参数传递给他们两个。我已为您更正如下,
DropdownButton<String>(
hint: "Select a color",
value: dropdownValue,
items: [
DropdownMenuItem<String>(
value : "green",
child: Row(
children: [
CircleAvatar(
backgroundColor: Colors.green,
radius: 10,
),
SizedBox(
width: 4,
),
Text(
"Green",
style: TextStyle(fontWeight: FontWeight.w600),
),
],
),
),
DropdownMenuItem<String>(
value : "blue",
child: Row(
children: [
CircleAvatar(
backgroundColor: Colors.blue,
radius: 10,
),
SizedBox(
width: 4,
),
Text(
"Blue",
style: TextStyle(fontWeight: FontWeight.w600),
),
],
),
),
DropdownMenuItem<String>(
value: "black",
child: Row(
children: [
CircleAvatar(
backgroundColor: Colors.black,
radius: 10,
),
SizedBox(
width: 4,
),
Text(
"Black",
style: TextStyle(fontWeight: FontWeight.w600),
),
],
),
),
DropdownMenuItem<String>(
value : "red",
child: Row(
children: [
CircleAvatar(
backgroundColor: Colors.red,
radius: 10,
),
SizedBox(
width: 4,
),
Text(
"Red",
style: TextStyle(fontWeight: FontWeight.w600),
),
],
),
),
],
onChanged: (val) {
if(val != null) {
setState((){
dropdownValue = value;});
}
},
)
并且请dropdownValue
在您的班级中声明字段为String? dropdownValue;
推荐阅读
- python - Python 从导入根包中引用子模块
- ag-grid - ag-grid nativescript-vue 组件未注册
- php - 如果超过两年,请检查帖子的发布日期
- emacs - 如何注册递归“加载路径”?
- python - Python 如何将列表的值与文件进行比较,然后将值保存到新文件中?
- python - 网页抓取文章 - 个人共同作者数据
- docker - 领事:将价值传递给游牧工作
- python - 在 macOS 上未找到已安装的 Python 模块
- excel - 从其父 BOM excel 修复子乘法
- typescript - 错误 TS2339:属性“_componentTag”不存在