DropDownButton Does not show the option that I chose

Issue

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

Answered By – Dali Hamza

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.