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

