I have a enum class. That is:
enum{
us,
uk,
in,
}
that enum class keeps the my country codes like at the above. But in the vscode showing that message:
'in' can't be used as an identifier because it's a keyword.
Try renaming this to be an identifier that isn't a keyword.
I wanna use the in like other ones. I gonna use that enum class for Api request. How can I use "in" with that class?
You can't use in
in your enum because in
is a reserved keyword for dart.
For example in dart you can write:
for (var item in items)
That's the reason why you can not call an identifier in
See Keywords for details.
My suggestion would be to use the whole country name in your enum
enum Country{
none,
usa,
unitedKingdoms,
india,
}
instead of the codes. That also makes it easier while programming for those not knowing all the codes. For passing it into the API, presumably as a string, you could use an extention method
extension CountryFunctionalities on Country{
String get asCountryCode {
switch (this) {
case Country.usa: return "us";
case Country.unitedKingdoms: return "uk";
case Country.india: return "in";
default: return "";
}
}
}
and use it like
Country countryInstance = Country.india;
print(countryInstance.asCountryCode); // output: "in"
Alternatively you could use the Alpha-3 Code "IND" instad of the Alpha-2 Code "IN". https://www.iban.com/country-codes
Lastly you could simply use "IN" instead of "in" as a name, that is not a reserved keyword.
Additional to harlekintiger:
Flutter 3 supports writing the enum like this too:
enum Country {
usa('us'),
unitedKingdoms('uk'),
india('in');
const Country(this.cc);
final String cc;
}
final String cc = Country.uk.cc;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.