formatNumberForMobileDialing method
String
formatNumberForMobileDialing(
- PhoneNumber number,
- String regionCallingFrom,
- bool withFormatting
Returns a number formatted in such a way that it can be dialed from a mobile phone in a specific region. If the number cannot be reached from the region (e.g. some countries block toll-free numbers from being called outside of the country), the method returns an empty string.
number
the phone number to be formatted.
regionCallingFrom
the region where the call is being placed.
withFormatting
whether the number should be returned with
formatting symbols, such as spaces and dashes.
Implementation
String formatNumberForMobileDialing(
PhoneNumber number, String regionCallingFrom, bool withFormatting) {
int countryCallingCode = number.countryCode;
if (!_hasValidCountryCallingCode(countryCallingCode)) {
return number.hasRawInput() ? number.rawInput : '';
}
String formattedNumber = '';
// Clear the extension, as that part cannot normally be dialed together with
// the main number.
PhoneNumber numberNoExt = number.deepCopy();
numberNoExt.clearExtension_3();
String regionCode = getRegionCodeForCountryCode(countryCallingCode);
PhoneNumberType numberType = getNumberType(numberNoExt);
bool isValidNumber = (numberType != PhoneNumberType.unknown);
if (regionCallingFrom == regionCode) {
bool isFixedLineOrMobile = (numberType == PhoneNumberType.fixedLine) ||
(numberType == PhoneNumberType.mobile) ||
(numberType == PhoneNumberType.fixedLineOrMobile);
// Carrier codes may be needed in some countries. We handle this here.
if (regionCode == 'BR' && isFixedLineOrMobile) {
formattedNumber =
// Historically, we set this to an empty string when parsing with raw
// input if none was found in the input string. However, this doesn't
// result in a number we can dial. For this reason, we treat the empty
// string the same as if it isn't set at all.
numberNoExt.preferredDomesticCarrierCode.isNotEmpty
? formatNationalNumberWithPreferredCarrierCode(numberNoExt, '')
:
// Brazilian fixed line and mobile numbers need to be dialed with a
// carrier code when called within Brazil. Without that, most of the
// carriers won't connect the call. Because of that, we return an
// empty string here.
'';
} else if (countryCallingCode == _nanpaCountryCode) {
// For NANPA countries, we output international format for numbers that
// can be dialed internationally, since that always works, except for
// numbers which might potentially be short numbers, which are always
// dialled in national format.
PhoneMetadata regionMetadata =
getMetadataForRegion(regionCode: regionCallingFrom)!;
if (canBeInternationallyDialled(numberNoExt) &&
_testNumberLength(getNationalSignificantNumber(numberNoExt),
regionMetadata) !=
ValidationResult.tooShort) {
formattedNumber =
format(numberNoExt, PhoneNumberFormat.international);
} else {
formattedNumber = format(numberNoExt, PhoneNumberFormat.national);
}
} else {
// For non-geographical countries, and Mexican, Chilean and Uzbek fixed
// line and mobile numbers, we output international format for numbers
// that can be dialed internationally as that always works.
if ((regionCode == regionCodeForNonGeoEntity ||
// MX fixed line and mobile numbers should always be formatted in
// international format, even when dialed within MX. For national
// format to work, a carrier code needs to be used, and the correct
// carrier code depends on if the caller and callee are from the
// same local area. It is trickier to get that to work correctly than
// using international format, which is tested to work fine on all
// carriers.
// CL fixed line numbers need the national prefix when dialing in the
// national format, but don't have it when used for display. The
// reverse is true for mobile numbers. As a result, we output them in
// the international format to make it work.
// UZ mobile and fixed-line numbers have to be formatted in
// international format or prefixed with special codes like 03, 04
// (for fixed-line) and 05 (for mobile) for dialling successfully
// from mobile devices. As we do not have complete information on
// special codes and to be consistent with formatting across all
// phone types we return the number in international format here.
((regionCode == 'MX' ||
regionCode == 'CL' ||
regionCode == 'UZ') &&
isFixedLineOrMobile)) &&
canBeInternationallyDialled(numberNoExt)) {
formattedNumber =
format(numberNoExt, PhoneNumberFormat.international);
} else {
formattedNumber = format(numberNoExt, PhoneNumberFormat.national);
}
}
} else if (isValidNumber && canBeInternationallyDialled(numberNoExt)) {
// We assume that short numbers are not diallable from outside their region,
// so if a number is not a valid regular length phone number, we treat it as
// if it cannot be internationally dialled.
return withFormatting
? format(numberNoExt, PhoneNumberFormat.international)
: format(numberNoExt, PhoneNumberFormat.e164);
}
return withFormatting
? formattedNumber
: normalizeDiallableCharsOnly(formattedNumber);
}