getCountryByFullname static method

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

Search by country full name: India, Cambodia, Canada

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

Implementation

static Future<Country> getCountryByFullname(String name,
    {CountryFilter? filter}) async {
  if (name.isNotEmpty) {
    var uri = "$_baseUrl" +
        Constants.countryByName +
        name +
        Constants.countryByFullname +
        "&&" +
        (filter.toFormattedUri != ""
            ? filter.toFormattedUri.substring(1, filter.toFormattedUri.length)
            : '');
    // print(uri);
    var response = await _client.get(Uri.parse(uri));

    if (response.statusCode == 200) {
      var countries = List<Country>.from(
          jsonDecode(response.body).map((x) => Country.fromJson(x)));
      return countries.first;
    }
    throw new Exception(
        "No country found. Please check if https://restcountries.eu is avialable.");
  } else {
    throw Exception("Country name can not be empty");
  }
}