PhoneNumber.fromCompleteNumber constructor

PhoneNumber.fromCompleteNumber({
  1. required String completeNumber,
})

Implementation

factory PhoneNumber.fromCompleteNumber({required String completeNumber}) {
  if (completeNumber == "") {
    return PhoneNumber(countryISOCode: "", countryCode: "", number: "");
  }

  try {
    Country country = getCountry(completeNumber);
    String number;
    if (completeNumber.startsWith('+')) {
      number = completeNumber
          .substring(1 + country.dialCode.length + country.regionCode.length);
    } else {
      number = completeNumber
          .substring(country.dialCode.length + country.regionCode.length);
    }
    return PhoneNumber(
        countryISOCode: country.code,
        countryCode: country.dialCode + country.regionCode,
        number: number);
  } on InvalidCharactersException {
    rethrow;
  } on Exception catch (e) {
    return PhoneNumber(countryISOCode: "", countryCode: "", number: "");
  }
}