首页 > 解决方案 > 颤动中的下拉按钮异常

问题描述

我正在使用FlutterDropdown按钮创建一个移动应用程序。发生下拉按钮异常。

'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) {},
)

有没有办法解决这个问题?期待很快可以收到你的来信。

标签: androidiosflutterdrop-down-menu

解决方案


两者DropdownButtonDropdownMenuItem具有value参数都是将显示为选定值的参数。DropdownMenuItem列表中应该只有一个valueitem不超过或少于一个)。这就是颤振所抱怨的。您忘记将该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;


推荐阅读