getCountry static method

Country getCountry(
  1. String phoneNumber
)

Implementation

static Country getCountry(String phoneNumber) {
  if (phoneNumber == "") {
    return countries.firstWhere((country) => country.code == "IN");
  }

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

  if (!validPhoneNumber.hasMatch(phoneNumber)) {
    return countries.firstWhere((element) => element.code == "IN");
  }

  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),
  );
}