Flutter DropdownButtonFormField 导致 AssertionError。

huangapple go评论61阅读模式
英文:

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(&#39;men&#39;),
            ),
            DropdownMenuItem(
              child: Text(&#39;women&#39;),
            )
          ],
          decoration: const InputDecoration(
              labelText: &#39;gender&#39;,
              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: &#39;birth date&#39;,
              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&lt;T&gt; item) =&gt; 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 values to your DropdownMenuItems


items: const [
  DropdownMenuItem(
    child: Text(&#39;men&#39;),
    value: 1,
  ),
  DropdownMenuItem(
    child: Text(&#39;women&#39;),
    value: 2,
  )
],

huangapple
  • 本文由 发表于 2023年4月17日 17:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定