'The instance member 'locale' can't be accessed in an initialize in the A class
I have a class A and also I have a delegate of class A. I have some getters like:
String get appTitle {
return localizedValues[locale.languageCode]!['welcome']!;
}
String get timeIsUpTitle {
return localizedValues[locale.languageCode]!['hello']!;
}
So here you can see the codes are duplicated. So I want to replace localizedValues[locale.languageCode]! this, to some value like selectedLanguage as an example. When I try this, it gives an error:
The instance member 'locale' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression
How can I solve this problem?
my class for example,
class A {
A(this.locale)
final Locale locale;
String get timeIsUpTitle {
return localizedValues[locale.languageCode]!['welcome']!;
}
some properties...
.
.
}
Also If you have any suggestion to avoid duplicate (string get string get......) I can take them.
Solution 1:[1]
You can do a method which will get that information using a paramenter:
class A {
A(this.locale);
final Locale locale;
String timeIsUpTitle2(String selectedLanguage) { // here pass the parameter you want to use
return localizedValues[selectedLanguage]!['welcome']!; // then get the value
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wilson Toribio |
