英文:
Flutter DropdownButtonFormField cause AssertionError
问题
I want to create a DropdownButtonFormField
and here is my code:
child: Row(
children: [
Flexible(
flex: 1,
child: DropdownButtonFormField(
onChanged: (value) {},
items: const [
DropdownMenuItem(
child: Text('men'),
),
DropdownMenuItem(
child: Text('women'),
)
],
decoration: const InputDecoration(
labelText: 'gender',
labelStyle: TextStyle(color: Color(0XFF121826)),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0XFF9B9B9B)),
borderRadius:
BorderRadius.all(Radius.circular(8))),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0XFF121826)),
borderRadius:
BorderRadius.all(Radius.circular(8))))),
),
const SizedBox(width: 16),
Flexible(
flex: 1,
child: TextFormField(
controller: TextEditingController(),
decoration: const InputDecoration(
labelText: 'birth date',
labelStyle: TextStyle(color: Color(0XFF121826)),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0XFF9B9B9B)),
borderRadius:
BorderRadius all(Radius.circular(8))),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0XFF121826)),
borderRadius:
BorderRadius all(Radius.circular(8)))),
keyboardType: TextInputType.text),
)
],
),
and as a result I get this:
Assertion failed: file:///home/.../dropdown.dart:1260:12 widget.items!.where((DropdownMenuItem<T> item) => item.value == widget.value).length == 1 is not true
how can I help this?
英文:
I want to create a DropdownButtonFormField
and here is my code:
child: Row(
children: [
Flexible(
flex: 1,
child: DropdownButtonFormField(
onChanged: (value) {},
items: const [
DropdownMenuItem(
child: Text('men'),
),
DropdownMenuItem(
child: Text('women'),
)
],
decoration: const InputDecoration(
labelText: 'gender',
labelStyle: TextStyle(color: Color(0XFF121826)),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0XFF9B9B9B)),
borderRadius:
BorderRadius.all(Radius.circular(8))),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0XFF121826)),
borderRadius:
BorderRadius.all(Radius.circular(8))))),
),
const SizedBox(width: 16),
Flexible(
flex: 1,
child: TextFormField(
controller: TextEditingController(),
decoration: const InputDecoration(
labelText: 'birth date',
labelStyle: TextStyle(color: Color(0XFF121826)),
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0XFF9B9B9B)),
borderRadius:
BorderRadius.all(Radius.circular(8))),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Color(0XFF121826)),
borderRadius:
BorderRadius.all(Radius.circular(8)))),
keyboardType: TextInputType.text),
)
],
),
and as a result I get this:
Assertion failed: file:///home/.../dropdown.dart:1260:12
widget.items!.where((DropdownMenuItem<T> item) => item.value == widget.value).length == 1
is not true
how can I help this?
答案1
得分: 1
尝试为您的DropdownMenuItem添加唯一的value
值
items: const [
DropdownMenuItem(
child: Text('men'),
value: 1,
),
DropdownMenuItem(
child: Text('women'),
value: 2,
)
],
英文:
Try adding unique value
s to your DropdownMenuItem
s
items: const [
DropdownMenuItem(
child: Text('men'),
value: 1,
),
DropdownMenuItem(
child: Text('women'),
value: 2,
)
],
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论