2021年2月10日星期三

Flutter dropdownButton setState on change not updating dropdown text after selection to show selected item

I created a dropdownButton to allow users to select from a dropdown list which will be populated from an API. For now I am populating using a list I created.

Currently the button is displaying the items from my list but after a selection has been made the list doesnt show the selected item and still shows the hint text. What I would like to happen is after a selection has been made then the dropdownButton shows the item that was selected instead of the hint text.

in the onChanged method I added a setState in hopes of updating the _selectedValue variable to the value that was selected and displaying it in the dropdownButton.

I also added a print statement in my setState method and that does trigger and show me the value within the value variable.

Here is my code.

List<DropdownMenuItem<int>> listItems = [DropdownMenuItem(child: Text("2016"), value: 2016,), DropdownMenuItem(child: Text("2021"), value: 2021,)];      int _selectedValue;    body: DropdownButton(            value: _selectedValue,            items: listItems,            hint: Text("Select Year"),            onChanged: (int value){             setState(() {               _selectedValue = value;               print(value);             });            },          ),  
https://stackoverflow.com/questions/66147510/flutter-dropdownbutton-setstate-on-change-not-updating-dropdown-text-after-selec February 11, 2021 at 09:23AM

没有评论:

发表评论