PhoneNumber.fromCompleteNumber constructor

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

Implementation

factory PhoneNumber.fromCompleteNumber({required String completeNumber}) {
  if (completeNumber == "") {
    return PhoneNumber(countryISOCode: "", countryCode: "", number: "");
  }
  try {
    String number;
    Country country = getCountry(completeNumber);
    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 {
    return PhoneNumber(
      countryISOCode: "",
      countryCode: "",
      number: completeNumber,
    );
    // ignore: unused_catch_clause
  } on Exception catch (e) {
    return PhoneNumber(
      countryISOCode: "",
      countryCode: "",
      number: completeNumber,
    );
  }
}