I want to develop a drop down button in Flutter. But I am getting Following Error.
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════ The following NoSuchMethodError was thrown building DefaultTextStyle(debugLabel: (englishLike body1 2014).merge(whiteMountainView body1), inherit: false, color: Color(0xffffffff), family: Roboto, size: 14.0, weight: 400, baseline: alphabetic, decoration: TextDecoration.none, softWrap: wrapping at box width, overflow: clip):
The getter 'value' was called on null. Receiver: null Tried calling: value
Here is my code.
String Selected_Category;
List<String>Categories=["C++","Java","Flutter","Kotlin","PHP","C#"];
DropdownButton<String>(
focusColor: Colors.redAccent,
items: Categories.map(
(String dropdownStringItem) {
DropdownMenuItem<String>(
value: dropdownStringItem,
child:
Text(dropdownStringItem),
);
}).toList(),
onChanged: (value) {
setState(() {
this.Selected_Category = value;
});
},
value: Selected_Category,
),
Please Help me that how can i solve this problem.
Thanks in Advance.
You get this error because your Selected_Category
variable is not initialized.
Give it a default value and you will good :)
Here is how you build a dropdown button:
String dropdownValue = 'One';
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(
color: Colors.deepPurple
),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
);
}
You are getting this error because you are not returning DropDownButton.
You are just missing return keyword.
return DropdownMenuItem<String>( // return keyword added
You can set value: 'c++' or initialise selected_category to either one option like
Selected_category = 'c++'
User contributions licensed under CC BY-SA 3.0