I have a widget that is called in my main scaffolding file later. This widget contains a dropdown menu but, I cannot change the state when selecting another value. The field does not update and I get the error message 'error: Method not found: 'setState'. setState(() {' ^^^^^^^^
I have updated the setState method and removed code from it but, it still says the method not found.
child: DropdownButton(
hint: Text('Medical'),
value: _selectedCustomerType,
onChanged: (newValue) {
setState(() {
_selectedCustomerType = newValue;
});
},
items: _customerType.map((cusType) {
print(cusType);
return DropdownMenuItem(
child: Text(cusType),
value: cusType,
);
}).toList(),
),
I need to be able to update the value and display it when the new value is chosen.
You can't use setState outside of a StatefulWidget so you should wrap your DropdownButton in a StatefulWidget, for example:
class StatefulDropdownButton extends StatefulWidget {
final List<String> _customerType;
StatefulDropdownButton(this._customerType);
@override
State<StatefulWidget> createState() => DropdownButtonState();
}
class DropdownButtonState extends State<StatefulDropdownButton> {
String _selectedCustomerType;
@override
Widget build(BuildContext context) {
return DropdownButton(
hint: Text('Medical'),
value: _selectedCustomerType,
onChanged: (newValue) {
setState(() {
_selectedCustomerType = newValue;
});
},
items: widget._customerType.map((cusType) {
print(cusType);
return DropdownMenuItem(
child: Text(cusType),
value: cusType,
);
}).toList(),
);
}
}