getCountriesByName static method

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

Search by country name

You can also pass incomplete country name

For example to search Countries whose names start with Ameri

Future<List<Country>> getCountriesByName(){
 try{
   List<Country> result = await CountryProvider.getCountriesByName("Ameri")
   return result;
  } catch(error) {
   return null;
 }
}

Implementation

static Future<List<Country>?> getCountriesByName(String name,
    {CountryFilter? filter}) async {
  if (name.isNotEmpty) {
    var uri =
        "$_baseUrl" + Constants.countryByName + name + filter.toFormattedUri;
    // 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;
    }
    throw new Exception(
        "No country found. Please check if https://restcountries.eu is avialable.");
  } else {
    throw Exception("Country name can not be empty");
  }
}