getCountryDataByPhone static method
Try to guess country from phone
Implementation
static CountryWithPhoneCode? getCountryDataByPhone(
final String phone, {
int? subscringLength,
}) {
/// If number is empty, return null
if (phone.isEmpty) {
return null;
}
/// Unless otherwise specified, start trying to match from the
/// end of the inputted phone string.
subscringLength = subscringLength ?? phone.length;
/// Must provide valid offset to start searching from
if (subscringLength < 1) {
return null;
}
final phoneCode = phone.substring(0, subscringLength);
try {
final countries = CountryManager().countries;
final retCountry = countries.firstWhere((final data) {
final res =
_toNumericString(data.phoneCode) == _toNumericString(phoneCode);
return res;
});
return retCountry;
} catch (_) {
return getCountryDataByPhone(phone, subscringLength: subscringLength - 1);
}
}