CodeFactor Codecov CodeRabbit Dart Code Metrics CI checks Pub points Last commit GitHub stars License: MIT Pub package

This ISO-driven, fully tested and pure Dart package provides information about world countries in the form of compile-time, tree-shakable constant classes with a sealed origin. Contains all 249+1 countries with ISO 3166 codes, their English, native names, emoji flags, capitals, population, postal codes, phone codes, languages (and scripts), currencies, country/currency/language name translations, etc. For Flutter ready widgets, please use world_countries package.

Features

WorldCountry class provides the following information about countries:

Field Required Description Example for CountryIrl
name Yes The English name of the country. CountryName(language: LangEng(), common: "Ireland", official: "Republic of Ireland")
namesNative Yes^1 The native names of the country. `CountryName`(language: `LangEng`(), official: "Republic of Ireland", common: "Ireland"), `CountryName`(language: `LangGle`(), official: "Poblacht na hÉireann", common: "Éire")
tld Yes The top-level domain names for the country. ".ie"
code Yes The three-letter ISO 3166-1 Alpha-3 code of the country. "IRL"
codeNumeric Yes The three-digit ISO 3166-1 Numeric code of the country. "372"
codeShort Yes The two-letter ISO 3166-1 Alpha-2 code of the country. "IE"
cioc No The International Olympic Committee code of the country. "IRL"
independent Yes Whether the country is an independent state. true
unMember Yes Whether the country is a member of the United Nations. true
currencies Yes^1 The currencies used in the country. [FiatEur()]
idd Yes The international direct dialing codes for the country. Idd(root: 3, suffixes: 53)
altSpellings Yes The alternate spellings of the country name. "IE", "Éire", "Republic of Ireland", "Poblacht na hÉireann"
continent Yes The continent where the country is located. Europe()
subregion No The subregion where the country is located. NorthernEurope()
languages Yes^1 The official languages spoken in the country. [LangEng(), LangGle()]
latLng Yes The geographic coordinates of the country. LatLng(53, -8)
landlocked Yes Whether the country is landlocked. false
bordersCodes No The codes of the countries that share borders with this country. "GBR"
areaMetric Yes The area of the country in square kilometers. 70273
demonyms Yes^1 The demonym names for the people of the country. `Demonyms`(language: `LangEng`(), female: "Irish", male: "Irish"), `Demonyms`(language: `LangFra()`, female: "Irlandaise", male: "Irlandais")
emoji Yes The emoji flag for the country. 🇮🇪
maps Yes The maps of the country. Maps(googleMaps: "hxd1BKxgpchStzQC6", openStreetMaps: "relation/62273")
population Yes The population of the country. 4994724
gini No The Gini coefficient of the country. Gini(year: 2017, coefficient: 31.4)
fifa No The FIFA code of the country. "IRL"
car Yes The car information of the country. Car(sign: "IRL", isRightSide: false)
timezones Yes The time zones of the country. "UTC+00:00"
capitalInfo No The capital city information of the country. CapitalInfo(capital: Capital("Dublin"), latLng: LatLng(53.32, -6.23))
hasCoatOfArms Yes Whether the country has an official coat of arms. true
postalCode Yes The postal code information of the country. PostalCode with format and regExp
startOfWeek Yes The first day of the week in the country. Weekday.monday
regionalBlocs No The regional blocs of the country. `BlocEU`()
translations Yes^1 The translations of the country name. List of 135+ TranslatedNames in different languages

Provides a compile-time constant of all countries accessible via WorldCountry.list moreover, the WorldCountry class provides the following methods/constructors:

  • maybeFromAnyCode - returns a country instance if the value matches any ISO 3166 code, otherwise returns null.
  • fromAnyCode - returns a country instance if the value matches any ISO 4217 code.
  • maybeFromValue - returns a country instance if the value matches the provided value, otherwise returns null.
  • fromCode - returns a country instance if the value matches the provided ISO 3166-1 Alpha-3 code.
  • fromCodeShort - returns a country instance if the value matches the provided ISO 3166-1 Alpha-2 code.
  • fromCodeNumeric - returns a country instance if the value matches the provided ISO 3166-1 numeric code.
  • maybeFromCode - returns a country instance if the value matches the provided ISO 3166-1 Alpha-3 code, otherwise returns null.
  • maybeFromCodeShort - returns a country instance if the value matches the provided ISO 3166-1 Alpha-2 code, otherwise returns null.
  • maybeFromCodeNumeric - returns a country instance if the value matches the provided ISO 3166-1 numeric code, otherwise returns null.
  • permissive - allows the creation of custom class instances that are not fully compatible with the ISO standard.

and functional-style like methods: whenOrNull, maybeWhen, when, map, maybeMap and is* boolean getters. You can also find many common methodsyou may know from Dart ecosystem - toString overrides, copyWith, toJson, compareTo, etc. Also, a compile-time const, tree-shakable, case-insensitive code maps (for a 0(1) access time code mapping), list and much more.

Translations: Use maybeCommonNameFor() or commonNameFor() methods to get translations for specific locale.

Getting started

To use this package, add sealed_countries as a dependency in your pubspec.yaml file.

dependencies:
  sealed_countries: any

Then import the package in your Dart code:

import 'package:sealed_countries/sealed_countries.dart';

Usage

To get information about countries, use the WorldCountry class. You can construct instances directly (using const), use the class's factory constructors or static methods, or select a country from the WorldCountry.list constant.

  print(WorldCountry.list.length); // Prints: "250".

  final country = WorldCountry.fromCode("MEX");
  print(country.name.common); // Prints: "Mexico".
  print(country.isMex); // Prints: true.

  final europeanCountries = WorldCountry.list.where(
    (cnt) => cnt.continent is Europe,
  );
  print(europeanCountries); // Prints a list of European countries.

  // Prints German translations of all available regular countries.
  final germanNames = WorldCountry.list.commonNamesMap(
    options: const LocaleMappingOptions(
      mainLocale: BasicTypedLocale(LangDeu()),
    ),
  );

  print(
    """Fully translated to German: ${germanNames.length == WorldCountry.list.length}""",
  ); // Prints: "Fully translated to German: true".
  for (final deuTranslation in germanNames.entries) {
    print("German name of ${deuTranslation.key.name}: ${deuTranslation.value}");
  }

Important

For specific ISO instances and their collections, e.g., const value = CountryChn(), const items = [CountryChn()] — prioritize compile-time constant references over var/final to ensure canonicalization and benefit from compiler optimizations.

For more usage examples, please see the /example folder.

FAQ

  • Classes with a sealed origin: This package provides data via classes with a sealed origin, defining specific permitted direct subclasses. This lets you use instances of these subclasses and customize their data or behavior (e.g., overriding methods), offering more structured flexibility than enums or standard open classes.
  • No 3rd-party dependencies: This package has no third-party dependencies, ensuring that you won't have any issues or conflicts with other dependencies (no even meta here, because of that).
  • Rich data: This package offers far more data than any other package + tons of translations (all GlobalMaterialLocalizations and GlobalCupertinoLocalizations locales and more).
  • Type-safe: The contracts and types in this package are exceptionally strong, ensuring that your code is strongly typed and well-defined.
  • High code coverage: The code in this package has 100% code coverage, with more than 1K (+2K in underlying packages) tests, providing confidence in its reliability and stability.
  • Comprehensive documentation: This package provides full documentation for every non-code generated public member, usually with examples, ensuring clarity and ease of use.
  • Mirrored Repository: The GitHub repository, including all package tags, is mirrored on GitLab, providing an alternative access point should GitHub become unavailable.
  • Industry adopted: This package is actively used in production by numerous European companies, ensuring its efficacy and robustness in real-world scenarios.
  • MIT license: This package and sources are released under the MIT license, which is a permissive license that allows users to use, modify, and distribute the code with minimal restrictions. The MIT license is considered better than most other open-source licenses because it provides flexibility and allows users to incorporate the code into their projects without worrying about legal implications.

Additional information

If you like this package, please give it a star or like. For more information on using this package, check out the API documentation. PRs or ideas are always welcome. If you have any issues or suggestions for the package, please file them in the GitHub repository.

References, credits and sources

^1: For the standard ISO classes specified in the relevant ISO standard. ^2: Translated JSON data to Dart language (following Effective Dart: Style guidelines), added additional data and functionality in Dart classes.

Libraries

country_translations
Provides country translations for sealed_countries.
sealed_countries
Provides data for world countries in the form of sealed classes.