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

时间:2020-06-25 06:15:29

标签: flutter

我实施了一个下拉菜单,在其中我们选择了大学。基于大学,您可以选择,例如:如果选择了孟买大学,则仅显示“计算机工程”来选择部门,而对于浦那大学,则显示所有部门。

Dropdown菜单正常工作,但菜单中选择的值不会显示。

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

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'

这是我实现的代码:

 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;
    });
  }
              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,),
                ]


                ,),

1 个答案:

答案 0 :(得分:1)

我希望这将对调用_changeDept()有所帮助,不要忘记将部门更新为默认值Select或其他内容。对于您的情况,您应该在valuechanged()中进行更改。如果您不想使用Select作为默认值,_selectedDeptVal = _getDeptItem(_selectedUniVal).first

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();
    }
  }
}

Pune Mubai

[已编辑]

[Pune and Mechanical Engineering]被选择状态下,即使更改为[Mumbai],部门值仍持有[Mechanical Engineering]。实际上,[Mechanical Engineering]不在uom中。那就是问题所在。因此,您必须更新_selectedDeptVal