formatNationalNumberWithCarrierCode method

String formatNationalNumberWithCarrierCode(
  1. PhoneNumber number,
  2. String carrierCode
)

Formats a phone number in national format for dialing using the carrier as specified in the {@code carrierCode}. The {@code carrierCode} will always be used regardless of whether the phone number already has a preferred domestic carrier code stored. If {@code carrierCode} contains an empty string, returns the number in national format without any carrier code.

number the phone number to be formatted. carrierCode the carrier selection code to be used.

Implementation

String formatNationalNumberWithCarrierCode(
    PhoneNumber number, String carrierCode) {
  int countryCallingCode = number.countryCode;

  String nationalSignificantNumber = getNationalSignificantNumber(number);
  if (!_hasValidCountryCallingCode(countryCallingCode)) {
    return nationalSignificantNumber;
  }

  // Note getRegionCodeForCountryCode() is used because formatting information
  // for regions which share a country calling code is contained by only one
  // region for performance reasons. For example, for NANPA regions it will be
  // contained in the metadata for US.
  String regionCode = getRegionCodeForCountryCode(countryCallingCode);

  // Metadata cannot be null because the country calling code is valid.
  PhoneMetadata metadata =
      _getMetadataForRegionOrCallingCode(countryCallingCode, regionCode)!;

  String formattedExtension = _maybeGetFormattedExtension(
    number,
    metadata,
    PhoneNumberFormat.national,
  );

  String formattedNationalNumber = _formatNsn(
    nationalSignificantNumber,
    metadata,
    PhoneNumberFormat.national,
    carrierCode,
  );

  return _prefixNumberWithCountryCallingCode(
    countryCallingCode,
    PhoneNumberFormat.national,
    formattedNationalNumber,
    formattedExtension,
  );
}