Update country_code.dart

Added methods tryFromCountryCode and tryFromDialCode, which has nullable result
This commit is contained in:
Lukas Pierce
2024-04-14 18:10:41 +05:00
committed by GitHub
parent b77740619a
commit 484a495360

View File

@@ -39,6 +39,15 @@ class CountryCode {
return CountryCode.fromJson(jsonCode!);
}
static CountryCode? tryFromCountryCode(String countryCode) {
try {
return CountryCode.fromCountryCode(countryCode);
} catch (e) {
if (kDebugMode) print('Failed to recognize country from countryCode: $countryCode');
return null;
}
}
factory CountryCode.fromDialCode(String dialCode) {
final Map<String, String>? jsonCode = codes.firstWhereOrNull(
(code) => code['dial_code'] == dialCode,
@@ -46,6 +55,15 @@ class CountryCode {
return CountryCode.fromJson(jsonCode!);
}
static CountryCode? tryFromDialCode(String dialCode)
try {
return CountryCode.fromDialCode(dialCode);
} catch (e) {
if (kDebugMode) print('Failed to recognize country from dialCode: $dialCode');
return null;
}
}
CountryCode localize(BuildContext context) {
return this
..name = CountryLocalizations.of(context)?.translate(code) ?? name;