getDescriptionForValidNumber method

String getDescriptionForValidNumber(
  1. PhoneNumber number,
  2. Locale languageCode, [
  3. String? userRegion
])

Returns a text description for the given phone number, in the language provided. The description might consist of the name of the country where the phone number is from, or the name of the geographical area the phone number is from if more detailed information is available.

This method assumes the validity of the number passed in has already been checked, and that the number is suitable for geocoding. We consider fixed-line and mobile numbers possible candidates for geocoding.

number a valid phone number for which we want to get a text description languageCode the language code for which the description should be written userRegion the region code for a given user. This region will be omitted from the description if the phone number comes from this region. It should be a two-letter upper-case CLDR region code. returns a text description for the given language code for the given phone number, or an empty string if the number could come from multiple countries, or the country code is in fact invalid

Implementation

String getDescriptionForValidNumber(PhoneNumber number, Locale languageCode,
    [String? userRegion]) {
  if (userRegion == null) {
    String langStr = languageCode.language;
    String scriptStr = ""; // No script is specified
    String regionStr = languageCode.country;

    String? areaDescription;
    String mobileToken =
        PhoneNumberUtil.getCountryMobileToken(number.countryCode);
    String nationalNumber = _phoneUtil.getNationalSignificantNumber(number);

    if (mobileToken != "" && nationalNumber.startsWith(mobileToken)) {
      // In some countries, eg. Argentina, mobile numbers have a mobile token before the national
      // destination code, this should be removed before geocoding.
      nationalNumber = nationalNumber.substring(mobileToken.length);
      String region =
          _phoneUtil.getRegionCodeForCountryCode(number.countryCode);
      PhoneNumber copiedNumber;
      try {
        copiedNumber = _phoneUtil.parse(nationalNumber, region);
      } on NumberParseException catch (_) {
        // If this happens, just reuse what we had.
        copiedNumber = number;
      }
      areaDescription = _prefixFileReader.getDescriptionForNumber(
          copiedNumber, langStr, scriptStr, regionStr);
    } else {
      areaDescription = _prefixFileReader.getDescriptionForNumber(
          number, langStr, scriptStr, regionStr);
    }

    return (areaDescription.isNotEmpty)
        ? areaDescription
        : _getCountryNameForNumber(number, languageCode);
  } //
  else {
    // If the user region matches the number's region, then we just show the lower-level
    // description, if one exists - if no description exists, we will show the region(country) name
    // for the number.
    String? regionCode = _phoneUtil.getRegionCodeForNumber(number);
    if (userRegion == regionCode) {
      return getDescriptionForValidNumber(number, languageCode);
    }
    // Otherwise, we just show the region(country) name for now.
    return _getRegionDisplayName(regionCode, languageCode);
    // TODO: Concatenate the lower-level and country-name information in an appropriate
    // way for each language.
  }
}