Flutter 在下拉菜单中显示值时出错

Flutter 在下拉菜单中显示值时出错,flutter,Flutter,我已经实现了一个下拉菜单,其中我们选择了大学。基于大学,你可以选择,例如:如果孟买大学被选中,只有计算机工程被选为系,而浦那大学则显示所有部门。 下拉菜单工作正常,但不显示菜单中选择的值 当我实现参数值时,我得到以下错误: There should be exactly one item with [DropdownButton]'s value: . Either zero or 2 or more [DropdownMenuItem]s were detected with the sam

我已经实现了一个下拉菜单,其中我们选择了大学。基于大学,你可以选择,例如:如果孟买大学被选中,只有计算机工程被选为系,而浦那大学则显示所有部门。

下拉菜单工作正常,但不显示菜单中选择的值

当我实现参数值时,我得到以下错误:

There should be exactly one item with [DropdownButton]'s value: . 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 827 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'
应该只有一个项目具有[DropdownButton]的值:。
检测到零个或2个或更多具有相同值的[DropdownMenuItem]
“包装:颤振/src/material/dropdown.dart”:
断言失败:第827行第15位:“items==null | | | items.isEmpty | | value==null||
项目。其中((DropdownMenuItem项目){
返回项。值==值;
}).length==1'
这是我实现的代码:

 var _universities = ["Savitribai Phule Pune University" , "University of Mumbai" , "other"];
List<DropdownMenuItem<String>> menuitems = List();

  final sppu = {
    "1" : "Computer Engineering",
    "2" : "Electronics & Telecommunications",
    "3" : "Information Technology",
    "4" : "Mechanical Engineering"
  };
  final uom = {
    "1" : "Computer Engineering",
  };
  final other = {
    "1" : "Inactive",
  };

  void populatesppu(){
    for(String key in sppu.keys){
      menuitems.add(DropdownMenuItem<String>(
        value: sppu[key],
        child: Center(
          child: Text(
            sppu[key],
          ),
        ),
      ));
    }
  }
  void populateuom(){
    for(String key in uom.keys){
      menuitems.add(DropdownMenuItem<String>(
        value: uom[key],
        child: Center(
          child: Text(
            uom[key],
          ),
        ),
      ));
    }
  }
  void populateother(){
    for(String key in other.keys){
      menuitems.add(DropdownMenuItem<String>(
        value: other[key],
        child: Center(
          child: Text(
            other[key],
          ),
        ),
      ));
    }
  }
  void valuechanged(_value){
    if(_value == "Savitribai Phule Pune University"){
      menuitems = [];
      populatesppu();
    }else if(_value == "University Of Mumbai"){
      menuitems = [];
      populateuom();
    }else if(_value == "Other"){
      menuitems = [];
      populateother();
    }
    setState(() {
      value = _value;
      _currentval = _value;
      disabledropdown = false;
    });
  }

  void secondvaluechanged(_value){
    setState(() {
      value = _value;
      _currentval2 =_value;
    });
  }
var_universions=[“萨维特里白普勒浦那大学”、“孟买大学”、“其他”];
List menuitems=List();
最终sppu={
“1”:“计算机工程”,
“2”:“电子与电信”,
“3”:“信息技术”,
“4”:“机械工程”
};
最终计量单位={
“1”:“计算机工程”,
};
最终其他={
“1”:“不活动”,
};
void populatesppu(){
for(sppu.keys中的字符串键){
添加(DropdownMenuItem(
值:sppu[键],
儿童:中心(
子:文本(
sppu[键],
),
),
));
}
}
无效人口(){
for(以计量单位为单位的字符串键){
添加(DropdownMenuItem(
值:计量单位[键],
儿童:中心(
子:文本(
计量单位[键],
),
),
));
}
}
void populateother(){
for(其他.keys中的字符串键){
添加(DropdownMenuItem(
值:其他[键],
儿童:中心(
子:文本(
其他[关键],
),
),
));
}
}
无效值已更改(_值){
if(_值==“萨维特里白普勒浦那大学”){
menuitems=[];
populatesppu();
}否则如果(_值==“孟买大学”){
menuitems=[];
populateum();
}如果(_值==“其他”){
menuitems=[];
populateother();
}
设置状态(){
值=_值;
_currentval=_值;
disabledropdown=false;
});
}
void secondvaluechanged(_值){
设置状态(){
值=_值;
_currentval2=_值;
});
}
列(
儿童:[
容器(
装饰:盒子装饰(
颜色:bgColor,
),
填充:仅限边缘设置(左:20,上:20,右:20),
孩子:表格(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(“大学详情”,
样式:TextStyle(颜色:颜色。白色,字体大小:22,字体重量:字体重量。粗体),
尺寸箱(高度:10,),
填充物(
填充:所有边缘设置(5.0),
孩子:填充(
填充:常数边集全部(8.0),
子:容器(
填充:仅限边设置(左:12.0,右:12.0),
装饰:盒子装饰(
边框:边框。全部(颜色:彩色。白色,宽度:2.0),
边界半径:边界半径。圆形(12.0),
),
孩子:下拉按钮(
项目:[
下拉菜单项(
价值:“萨维特里白普勒浦那大学”,
儿童:中心(
儿童:文本(“萨维特里白普勒浦那大学”),
),
),
下拉菜单项(
价值观:“孟买大学”,
儿童:中心(
儿童:文本(“孟买大学”),
),
),
下拉菜单项(
值:“其他”,
儿童:中心(
子项:文本(“其他”),
),
)
],
一旦更改:(_值)=>值更改(_值),
提示:文本(“选择大学”,
样式:TextStyle(颜色:Colors.white),),
标高:5,
图标:图标(图标。箭头下拉,颜色:颜色。黑色,),
iconSize:36,
是的,
样式:TextStyle(颜色:Colors.black,字体大小:20),
//值:_currentval,
//价值:价值,
),
),
),
),
              Column(
                children: <Widget>[
                  Container(
                      decoration: BoxDecoration(
                          color: bgColor,
                          ),
                      padding: EdgeInsets.only(left: 20,top: 20,right: 20),
                      child: Form(

                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text("University Details",
                              style: TextStyle(color: Colors.white,fontSize: 22,fontWeight: FontWeight.bold),),
                            SizedBox(height: 10,),
                            Padding(
                              padding: EdgeInsets.all(5.0),
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Container(
                                  padding: EdgeInsets.only(left: 12.0,right: 12.0),
                                  decoration: BoxDecoration(
                                    border: Border.all(color: Colors.white,width: 2.0),
                                    borderRadius: BorderRadius.circular(12.0),
                                  ),
                                  child: DropdownButton<String>(
                                    items:[
                                      DropdownMenuItem<String>(
                                        value: "Savitribai Phule Pune University",
                                        
                                        child: Center(
                                        
                                          child: Text("Savitribai Phule Pune University"),
                                        ),
                                      ),
                                      DropdownMenuItem<String>(
                                        value: "University Of Mumbai",
                                        child: Center(
                                          child: Text("University Of Mumbai"),
                                        ),
                                      ),
                                      DropdownMenuItem<String>(
                                        value: "Other",
                                        child: Center(
                                          child: Text("Other"),
                                        ),
                                      )
                                    ],

                                    onChanged: (_value) => valuechanged(_value),

                                    hint:Text("SELECT UNIVERSITY",
                                    style: TextStyle(color: Colors.white),),
                                    elevation: 5,
                                    icon: Icon(Icons.arrow_drop_down,color: Colors.black,),
                                    iconSize: 36,
                                    isExpanded: true,
                                    style: TextStyle(color: Colors.black,fontSize: 20),
                                    //value: _currentval,
                                    //value: value,
                                    
                                  ),
                                ),
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.all(5.0),
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Container(
                                  padding: EdgeInsets.only(left: 12.0,right: 12.0),
                                  decoration: BoxDecoration(
                                    border: Border.all(color: Colors.white,width: 2.0),
                                    borderRadius: BorderRadius.circular(12.0),
                                  ),
                                  child: DropdownButton<String>(
                                    items:
                                    menuitems,
                                    onChanged: disabledropdown ? null : (_value) => secondvaluechanged(_value),
                                    
                                    hint: Text(
                                        "SELECT DEPARTMENT",
                                        style: TextStyle(color: Colors.white)),
                                    disabledHint: Text(
                                      "First Select Any University",
                                      style: TextStyle(color: Colors.white),
                                    ),
                                    elevation: 5,
                                    icon: Icon(Icons.arrow_drop_down,color: Colors.black,),
                                    iconSize: 36,
                                    isExpanded: true,
                                    style: TextStyle(color: Colors.black,fontSize: 18),
                                    //value: _currentval2,
                                    //value: value,
                                    
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(height: 4,),

                            Padding(
                              padding: const EdgeInsets.all(5.0),
                              child: Container(
                                padding: EdgeInsets.only(left: 12.0,right: 12.0),
                                child: TextFormField(
                                  decoration: InputDecoration(
                                    labelText: "College/Organization(in short)",
                                    labelStyle: TextStyle(color: Colors.white,fontSize: 19),
                                    
                                  ),
                                  style: TextStyle(color: Colors.white,fontSize: 23),
                                  onChanged: (val) {
                                    setState(() {
                                      name=val;
                                    });
                                  },
                                ),
                              ),
                            ),
                            SizedBox(height: 10,),
                            RaisedButton(
                              textColor: Colors.deepPurple,
                              onPressed: ()async {},
                                
                              color: Colors.white,
                              child: Text("ADD",
                                textAlign: TextAlign.center,
                                style: TextStyle(fontSize: 17,
                                    fontWeight: FontWeight.bold),
                              ),
                            ),
                            //Text("$value",),

                          ],
                        ),
                      )
                  ),

                  SizedBox(height: 20,),
                ]


                ,),
class _MyHomePageState extends State<MyHomePage> {
  final _universities = const [
    "Savitribai Phule Pune University",
    "University of Mumbai",
    "other",
  ];
  final sppu = const {
    "1": "Computer Engineering",
    "2": "Electronics & Telecommunications",
    "3": "Information Technology",
    "4": "Mechanical Engineering"
  };
  final uom = const {
    "1": "Computer Engineering",
  };
  final other = const {
    "1": "Inactive",
  };

  var _selectedUniVal;
  var _selectedDeptVal = 'Select';
  List<String> _deptItem = ['Select'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          padding: const EdgeInsets.all(20),
          child: Column(
            children: <Widget>[
              DropdownButton(
                value: _selectedUniVal,
                onChanged: (val) => _changeDept(currentUni: val),
                items: _universities.map(
                  (item) {
                    return DropdownMenuItem(
                      child: Text('$item'),
                      value: item,
                    );
                  },
                ).toList(),
              ),
              DropdownButton(
                value: _selectedDeptVal,
                onChanged: (val) => setState(() => _selectedDeptVal = val),
                items: _deptItem.map(
                  (item) {
                    return DropdownMenuItem(
                      child: Text('$item'),
                      value: item,
                    );
                  },
                ).toList(),
              ),
            ],
          ),
        ),
      ),
    );
  }

 void _changeDept({String currentUni}) {
    setState(
      () {
        // update current university
        _selectedUniVal = currentUni;
        // reset dept val
        _selectedDeptVal = 'Select';
        // update corresponding department
        // clear list
        _deptItem = ['Select'];
        _deptItem.addAll(_getDeptItem(_selectedUniVal));
      },
    );
  }

  List<String> _getDeptItem(String currentUni) {
    switch (currentUni) {
      case 'Savitribai Phule Pune University':
        return sppu.values.toList();
        break;
      case 'University of Mumbai':
        return uom.values.toList();
        break;
      case 'other':
      default:
        return other.values.toList();
    }
  }
}