I have a screen with a form that displays a drop-down list of counties. When the screen initially loads I want to set the default to the current country.
class _SignInScreenState extends State<SignInScreen> {
final formKey = new GlobalKey<FormState>();
String countryCode = _CountryCode();
_countryCode() {
Locale myLocale = Localizations.localeOf(context);
return myLocale.countryCode;
}
@override
Widget build(BuildContext context) {...
This results in the following error: "Only static members can be accessed in initializers". I researched this and it stated the solution was to initialise variables in initState(), as shown below:
@override
initState() {
super.initState();
countryCode = _countryCode();
}
This does not produce an error however in the widget tree the value of countryCode is null whereas in the widget tree _countryCode() displays "US" correctly.
If I set the value of countryCode in init states does this not mean it will be reset every time the widget tree is redrawn?