getCountryByCode static method

Future<Country> getCountryByCode(
  1. String code, {
  2. CountryFilter? filter,
})

Search by list of ISO 3166-1 2-letter or 3-letter country codes: Ind, Col, ru

Future<Country> getCountryByCode(){
 try{
   Country result = await CountryProvider.getCountryByCode("Ind")?.first;
   return result;
  } catch(error) {
   return null;
 }
}

Implementation

static Future<Country> getCountryByCode(String code,
    {CountryFilter? filter}) async {
  if (code.isNotEmpty) {
    var uri =
        "$_baseUrl" + Constants.countryByCode + code + filter.toFormattedUri;
    // print(uri);
    var response = await _client.get(Uri.parse(uri));

    if (response.statusCode == 200) {
      var country = Country.fromJson(jsonDecode(response.body));
      return country;
    }
    throw new Exception(
        "No country found. Please check if https://restcountries.eu is avialable.");
  } else {
    throw Exception("Country code can not be empty");
  }
}