Search code examples
flutterandroid-studioandroid-widgetflutter-dropdownbutton

DropDownButton Does not show the option that I chose


I am using Flutter and are planning to make a dropdownbutton for user to select from. Upon selecting one of the option it will still shows the hint instead of the option that the user chose.

this is the code:

DropdownButton<String>(
              isExpanded: true,
              items: <String>['student', 'driver'].map((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: new Text(value),
                );
              }).toList(),
              hint: new Text ("Select a Role"),
              onChanged: (value) => _role = value,
            ),

Thank you in advance for helping.


Solution

  • you should like this

    DropdownButton<String>(
                  isExpanded: true,
                  items: <String>['student', 'driver'].map((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: new Text(value),
                    );
                  }).toList(),
                  value:_role,
                  hint: new Text ("Select a Role"),
                  onChanged:  (value) => setState((){
                           _role = value
                       }),
                ),