selectedCountryCode static method

CountryModel? selectedCountryCode({
  1. required String countryCode,
  2. String phoneNumber = '',
})

Returns the country model for the given country code and phone number If the phone number is empty, the country model for the given country code is returned

Implementation

static CountryModel? selectedCountryCode({
  required String countryCode,
  String phoneNumber = '',
}) {
  try {
    CountryModel? selectedCountry;

    List<CountryModel> selectedCountries = rawCountries
        .where(
          (country) => country.intlDialCode == countryCode,
        )
        .toList();
    if (phoneNumber.isEmpty || selectedCountries.length == 1) {
      selectedCountry = selectedCountries.first;
    }

    for (final country in selectedCountries) {
      if (country.areaCodes == null || country.areaCodes!.isEmpty) {
        continue;
      } else {
        for (final region in country.areaCodes!) {
          if (phoneNumber.startsWith(region)) {
            selectedCountry = country;
          }
        }
      }
    }

    return selectedCountry;
  } catch (e) {
    return null;
  }
}