getCountry static method

Country getCountry(
  1. String phoneNumber
)

Implementation

static Country getCountry(String phoneNumber) {
  if (phoneNumber == "") {
    throw NumberTooShortException();
  }

  final validPhoneNumber = RegExp(r'^[+0-9]*[0-9]*$');

  if (!validPhoneNumber.hasMatch(phoneNumber)) {
    throw InvalidCharactersException();
  }

  if (phoneNumber.startsWith('+')) {
    return countries.firstWhere(
      (country) => phoneNumber
          .substring(1)
          .startsWith(country.dialCode + country.regionCode),
    );
  }
  return countries.firstWhere(
    (country) =>
        phoneNumber.startsWith(country.dialCode + country.regionCode),
  );
}