identifyCountryCode function
Implementation
PhoneNumberResult identifyCountryCode(
String phoneNumber, List<CountryDialCode> countryData) {
// Normalize the phone number
phoneNumber = phoneNumber.trim().replaceAll(' ', '').replaceAll('-', '');
// Check if the phone number starts with a '+'
if (!phoneNumber.startsWith('+')) {
// throw FormatException('Phone number must start with "+" sign');
}
// Try to match the phone number with country codes
for (var country in countryData) {
if (phoneNumber.startsWith('+' + country.phone)) {
// Strip the country code from the phone number
String strippedNumber =
phoneNumber.substring(('+' + country.phone).length);
return PhoneNumberResult(
countryDialCode: country, strippedNumber: strippedNumber);
}
}
// If no match is found, return null
return PhoneNumberResult(countryDialCode: null, strippedNumber: phoneNumber);
}