首页 > 解决方案 > 在下拉菜单中显示值时出错

问题描述

我已经实施了一个下拉菜单,我们在其中选择了大学。根据您获得的大学选项,例如:如果选择孟买大学,则仅显示计算机工程用于选择部门,而对于浦那大学,则显示所有部门。

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

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

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


                ,),

标签: flutter

解决方案


我希望这会有所帮助,当_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 and Mechanical Engineering]处于选中状态时,即使您更改为[Mumbai],部门值仍然保持[Mechanical Engineering]。其实[Mechanical Engineering]不在uom. 那就是问题所在。所以,你必须更新_selectedDeptVal.


推荐阅读