Flutter Drop DownMenuItem Widget

0

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.

flutter
drop-down-menu
flutter-layout

4 Answers

0

You get this error because your Selected_Category variable is not initialized.

Give it a default value and you will good :)

answered on Stack Overflow May 2, 2020 by F Perroch
0

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(),
  );
}
answered on Stack Overflow May 2, 2020 by gg11
0

You are getting this error because you are not returning DropDownButton.

You are just missing return keyword.

return DropdownMenuItem<String>( // return keyword added
answered on Stack Overflow May 2, 2020 by Viren V Varasadiya
0

You can set value: 'c++' or initialise selected_category to either one option like

         Selected_category = 'c++'
answered on Stack Overflow May 2, 2020 by Ajay Vaidhyanathan

User contributions licensed under CC BY-SA 3.0