Introduction
Hey Guys, Have you ever deal with managing countries data in your app like displaying countries name in dropdown menu, getting capital city, calling code, language, currency[name, code, symbol], filter information, etc. How you manage all these data?
If I talking of my self, I used to store these data either in a JSON file or created a list of map to display and manage countries info. Is it a correct approach?
There is a simple explanation, If you need to display only country name in a list or in the dropdown you can store countries name in a JSON file but when It comes to perform more operation like getting countries other information on the basis of one entity it is getting more complex and we have to write boilerplate code to filter data.
To keep code clean and avoid unnecessary boilerplate code I have develop a flutter plugin country_provider. This plugin provides countries info and several filters and search methods.
How country_provider plugin help you ?
The plugin provides several methods to fetch countries info, Apply filter, and perform search operations.
List of operations mentioned below:-
- Fetch all countries info. ex. Name, capital city, currency(Name, code and symbol), Language(Name and Code), Native name, Borders, Timezone, Flag, etc.
- Search by country name. It can be the native name or partial name.
- Search country by country full name.
- Search country by ISO 3166-1 2-letter or 3-letter country code.
- Search country by list of ISO 3166-1 2-letter or 3-letter country codes.
- Search country by ISO 4217 currency code.
- Search country by ISO 639-1 language code.
- Search country by capital city.
- Search country by calling code.
- Search country by continent: Africa, Americas, Asia, Europe, Oceania.
How plugin works
Country Provider is a flutter library wrapper around the API provided by REST Countries https://restcountries.eu Plugin gets countries information from rest API and provides search, filter mechanism.
How to use country_provider plugin in flutter app ?
1. Add library to your pubspec.yaml
dependencies:
country_provider: ^0.0.1
2. Import library in dart file
import 'package:country_provider/country_provider.dart';
Note
Each method returns List
of Country model.
Usage
- Get all countries.
// Get all countries
List<Country> countries = await CountryProvider.getAllCountries();
- Search by country name. It can be the native name or partial name.
// Search by country name
List<Country> result = await CountryProvider.getCountriesByName("Ameri");
If partial name, this method could return a list of countries. Else a List of one element.
- Search by country full name.
// Search by country full name
Country result = await CountryProvider.getCountryByFullname("India")?.first;
- Search by ISO 3166-1 2-letter or 3-letter country code.
// Search by list of ISO 3166-1 2-letter or 3-letter country codes
Country result = await CountryProvider.getCountryByCode("Ind")?.first;
- Search by list of ISO 3166-1 2-letter or 3-letter country codes.
// Search by list of ISO 3166-1 2-letter or 3-letter country codes
List<Country> result = CountryProvider.getCountriesByListOfCodes(["Ind", "col", "ru"]);
- Search by ISO 4217 currency code.
// Search by ISO 4217 currency code
List<Country> result = await CountryProvider.getCountryByCurrencyCode("Inr")
- Search by ISO 639-1 language code.
// Search by ISO 639-1 language code
List<Country> result = await CountryProvider.getCountriesByLanguageCode(["Hin","en",]);
- Search by capital city.
// Search by capital city
var result = await CountryProvider.getCountryByCapitalCity("Delhi");
You can use var
instead of explicit types. I use explicit types to show you the return type of each method.
- Search by calling code.
// Search by calling code
List<Country> result = await CountryProvider.getCountryByCallingCode(91);
- Search by continent: Africa, Americas, Asia, Europe, Oceania.
// Search by continent: Africa, Americas, Asia, Europe, Oceania
List<Country> result = await CountryProvider.getcountryByRegionalBloc("Asia");
- Search by regional bloc: EU, EFTA, CARICOM, AU, USAN, EEU, AL, ASEAN , CAIS, CEFTA , NAFTA , SAARC.
// Search by regional bloc
List<Country> result = await CountryProvider.getCountriesByContinent("ASEAN");
EU (European Union), EFTA (European Free Trade Association), CARICOM (Caribbean Community), PA (Pacific Alliance), AU (African Union), USAN (Union of South American Nations), EEU (Eurasian Economic Union), AL (Arab League), ASEAN (Association of Southeast Asian Nations), CAIS (Central American Integration System), CEFTA (Central European Free Trade Agreement), NAFTA (North American Free Trade Agreement), SAARC (South Asian Association for Regional Cooperation).
Apply filters
To get filtered country data pass CountryFilter model as argument in search countries method.
// Get all countries name only
var countries = await CountryProvider.getAllCountries(filter: CountryFilter(isName: true));
List<string> countriesInSpanish = countries.map((e) => e.name).toList();
// Get all countries name only in Spanish
var countries = await CountryProvider.getAllCountries(filter: CountryFilter(isName: true));
List<string> countriesInSpanish = countries.map((e) => e.translations.es).toList();
// Get Europe countries in French language
var europeCountries = await CountryProvider.getcountryByRegionalBloc("Europe",filter: CountryFilter(isName: true));
List<string> europeCountriesInFrench = europeCountries.map((e) => e.translations.fr).toList();
// Get all countries name with their capital city only
var countries = await CountryProvider.getAllCountries(filter: CountryFilter(isName: true,isCapital:true));
// Get all countries name with country code and their capital city only
var countries = await CountryProvider.getAllCountries(filter: CountryFilter(isName: true,isCapital:true,isAlpha2Code:true,isAlpha3Code: true));
// Fell free to aplly filters π€
The default language for country name is English, but you can also get the name in other languages such as: de
(German language), es
(Spanish language), fr
(French language), ja
(Japanese language), it
(Italian language), br
(Breton language), pt
(Portuguese language), nl
(Dutch language), hr
(Croatian language) and fa
(Persian language).
Country class
Each method return List
of Country
. You will get the exact idea of what country info includes by looking into the country model below.
class Country
{
// Get Country name
String name;
// Gets Top Level Domain
List<String> topLevelDomain;
// Gets Alpha2 Code
String alpha2Code;
// Gets Alpha3 Code
String alpha3Code;
// Gets Calling Code
List<String> callingCodes;
// Gets Capital City
String capital;
// Get AltSpelling
List<String> altSpellings;
// Get Region
String region;
// Get SubDomain
String subregion;
// Get Population
int population;
// Get Latlng(Latitude and Longitude)
List<double> latlng;
// Get Demonym
String demonym;
// Get Area
double area;
// Get Gini
double gini;
// Get Timezone
List<String> timezones;
// Get Borders
List<String> borders;
// Get Native Name
String nativeName;
// Get Numeric Code
String numericCode;
// Get Currencies
List<Currency> currencies;
// Get Languages
List<Language> languages;
// Gets Translations
Translations translations;
// Get Flag
String flag;
// Get Regional Blocs
List<RegionalBloc> regionalBlocs;
// Get Cioc(International Olympic Committee Code)
String cioc;
}
Links
- Get country_provider plugin from Here.
Click here to get Demo App
-
Plugin source code
TheAlphamerc / country_provider
A flutter plugin to provide information about countries.
country_provider Flutter Plugin
Country Provider is a flutter library wrapper around the API provided by REST Countries https://restcountries.eu (Get information about countries via a RESTful API)
Download App
Getting Started
1. Add library to your pubspec.yaml
dependencies country_provider: ^0.0.2
2. Import library in dart file
import 'package:country_provider/country_provider.dart';
Note
Each method return a
List
ofCountry
.Usage
- Get all countries.
// Get all countries List<Country>? countries = await CountryProvider.getAllCountries();
- Search by country name. It can be the native name or partial name.
// Search by country name List<Country>? result = await CountryProvider.getCountriesByName("Ameri");
If partial name, this method could return a list of countries. Else a List of one element.
- Search by country full name.
// Search by country full name Country result = await CountryProvider.getCountryByFullname("India")?
β¦
Wrapping it up
While there is much more you can do with country_provider plugin, this tutorial should help you get set up and ready to go with some basic operations. Be sure to check out the latest plugin version to get updated and more features. Code from this tutorial can be found on project repository on github.
If you have any questions or if this tutorial inspires you to build anything interesting let me know in comments. If you have any ideas for new features or wants to contribute in project be sure to send a pull request.
Top comments (0)