getRegionCodeForNumber method

String? getRegionCodeForNumber(
  1. PhoneNumber? number
)

Returns the region where a phone number is from. This could be used for geocoding at the region level. Only guarantees correct results for valid, full numbers (not short-codes, or invalid numbers).

number the phone number whose origin we want to know. getRegionCodeForNumber returns the region where the phone number is from, or null if no region matches this calling code.

Implementation

String? getRegionCodeForNumber(PhoneNumber? number) {
  if (number == null) return null;
  int countryCode = number.countryCode;

  List<String>? regions =
      _metadataMapLoader.countryCodeToRegionCodeMap['$countryCode'];
  if (regions == null || regions.isEmpty) return null;

  if (regions.length == 1) {
    return regions.first;
  } else {
    return _getRegionCodeForNumberFromRegionList(number, regions);
  }
}