I want my Dropdownbutton to change according to the item selected by the user. These are my variable declarations:
class _HomeState extends State<Home> {
final gb_controller = TextEditingController();
final brutto_controller = TextEditingController();
final bAV_controller = TextEditingController();
int steuerklassen_value = 1;
String selectedValue = "Steuerklasse";
String hintValue = "Steuerklasse 1";
bool loading = false;
final formkey = GlobalKey<FormState>();
final steuerklassen = ['I', 'II', 'III', 'IV', 'V', 'VI'];
and this is my DropDownButton:
child: DropdownButton<String>(
hint: Text(hintValue),
iconSize: 20,
items: steuerklassen
.map((String dropDownStringItem) {
return DropdownMenuItem(
value: dropDownStringItem,
child: Text(dropDownStringItem),
);
}).toList(),
onChanged: (selectedValue) {
setState(() {
for (int i = 1;
i <= steuerklassen.length;
i++)
if (steuerklassen[i] ==
selectedValue) {
this.steuerklassen_value = i + 1;
}
hintValue =
"Steuerklasse $selectedValue";
return steuerklassen_value;
});
},
),
I want the hint value to change when a new item is selected. I thought I do this with hintValue = "Steuerklasse $selectedValue";
but obviously not.
This is my app:
And the text on the drop-down button should change but it doesn't.
You can copy paste run full code below
Your code has runtime error
RangeError (index): Invalid value: Not in inclusive range
You can change from
to
working demo
full code