Error while displaying value in drop down menu

Viewed 98

I have implemented a dropdownmenu in which we have select university. Based on the university you get the option,for eg: if University of Mumbai is being selected only Computer Engineering is shown for selecting the department,while for University of Pune all the departments are shown.

Dropdownmenu works properly but the value selected in the menu is not displayed.

When i implement the parameter value i get the below error:

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'

This is the code i implemented:

 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 Answers

I hope this will help, when _changeDept() is called, don't forget to update department to default value Select or something. In your case, you should change in valuechanged(). In case you don't want to use Select as default value, _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

[Edited]

When [Pune and Mechanical Engineering] is selected state, even you change to [Mumbai], department value is still holding [Mechanical Engineering]. Actually, [Mechanical Engineering] isn't in the uom. That's the problem. So, you have to update _selectedDeptVal.

Related