formatNationalNumberWithPreferredCarrierCode method

String formatNationalNumberWithPreferredCarrierCode(
  1. PhoneNumber number,
  2. String fallbackCarrierCode
)

Formats a phone number in national format for dialing using the carrier as specified in the preferredDomesticCarrierCode field of the PhoneNumber object passed in. If that is missing, use the fallbackCarrierCode passed in instead. If there is no preferredDomesticCarrierCode, and the fallbackCarrierCode contains an empty string, return the number in national format without any carrier code.

Use [formatNationalNumberWithCarrierCode] instead if the carrier code passed in should take precedence over the number's [preferredDomesticCarrierCode] when formatting.

number the phone number to be formatted. fallbackCarrierCode the carrier selection code to be used, if none is found in the phone number itself.

Implementation

String formatNationalNumberWithPreferredCarrierCode(
    PhoneNumber number, String fallbackCarrierCode) {
  return formatNationalNumberWithCarrierCode(
    number,
    // 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.
    number.preferredDomesticCarrierCode.isNotEmpty
        ? number.preferredDomesticCarrierCode
        : fallbackCarrierCode,
  );
}