From 484a495360855f23104edace6e44afd7642f19e1 Mon Sep 17 00:00:00 2001 From: Lukas Pierce Date: Sun, 14 Apr 2024 18:10:41 +0500 Subject: [PATCH 1/2] Update country_code.dart Added methods tryFromCountryCode and tryFromDialCode, which has nullable result --- lib/src/country_code.dart | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/src/country_code.dart b/lib/src/country_code.dart index b8a3863..2339303 100644 --- a/lib/src/country_code.dart +++ b/lib/src/country_code.dart @@ -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? 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; From d9cf16a14849818f85919090f652958380c449d6 Mon Sep 17 00:00:00 2001 From: Lukas Date: Sun, 14 Apr 2024 22:20:09 +0500 Subject: [PATCH 2/2] fix --- lib/src/country_code.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/src/country_code.dart b/lib/src/country_code.dart index 2339303..3a8069f 100644 --- a/lib/src/country_code.dart +++ b/lib/src/country_code.dart @@ -1,5 +1,6 @@ import 'package:collection/collection.dart' show IterableExtension; import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart' show kDebugMode; import 'country_codes.dart'; import 'country_localizations.dart'; @@ -55,7 +56,7 @@ class CountryCode { return CountryCode.fromJson(jsonCode!); } - static CountryCode? tryFromDialCode(String dialCode) + static CountryCode? tryFromDialCode(String dialCode) { try { return CountryCode.fromDialCode(dialCode); } catch (e) {