sealed_countries 2.6.0

SDKdartflutter
Platformandroidioswindowslinuxmacosweb

Provides data for world countries in the form of sealed classes.

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:

FieldRequiredDescriptionExample for CountryIrl
nameYesThe English name of the country.CountryName(language: LangEng(), common: "Ireland", official: "Republic of Ireland")
namesNativeYes1The native names of the country.[CountryName(language: LangEng(), official: "Republic of Ireland", common: "Ireland"), CountryName(language: LangGle(), official: "Poblacht na hÉireann", common: "Éire")]
tldYesThe top-level domain names for the country.[".ie"]
codeYesThe three-letter ISO 3166-1 Alpha-3 code of the country."IRL"
codeNumericYesThe three-digit ISO 3166-1 Numeric code of the country."372"
codeShortYesThe two-letter ISO 3166-1 Alpha-2 code of the country."IE"
ciocNoThe International Olympic Committee code of the country."IRL"
independentYesWhether the country is an independent state.true
unMemberYesWhether the country is a member of the United Nations.true
currenciesYes1The currencies used in the country.[FiatEur()]
iddYesThe international direct dialing codes for the country.Idd(root: 3, suffixes: [53])
altSpellingsYesThe alternate spellings of the country name.["IE", "Éire", "Republic of Ireland", "Poblacht na hÉireann"]
continentYesThe continent where the country is located.Europe()
subregionNoThe subregion where the country is located.NorthernEurope()
languagesYes1The official languages spoken in the country.[LangEng(), LangGle()]
latLngYesThe geographic coordinates of the country.LatLng(53, -8)
landlockedYesWhether the country is landlocked.false
bordersCodesNoThe codes of the countries that share borders with this country.["GBR"]
areaMetricYesThe area of the country in square kilometers.70273
demonymsYes1The demonym names for the people of the country.[Demonyms(language: LangEng(), female: "Irish", male: "Irish"), Demonyms(language: LangFra(), female: "Irlandaise", male: "Irlandais")]
emojiYesThe emoji flag for the country.🇮🇪
mapsYesThe maps of the country.Maps(googleMaps: "hxd1BKxgpchStzQC6", openStreetMaps: "relation/62273")
populationYesThe population of the country.4994724
giniNoThe Gini coefficient of the country.Gini(year: 2017, coefficient: 31.4)
fifaNoThe FIFA code of the country."IRL"
carYesThe car information of the country.Car(sign: "IRL", isRightSide: false)
timezonesYesThe time zones of the country.["UTC+00:00"]
capitalInfoNoThe capital city information of the country.CapitalInfo(capital: Capital("Dublin"), latLng: LatLng(53.32, -6.23))
hasCoatOfArmsYesWhether the country has an official coat of arms.true
postalCodeYesThe postal code information of the country.PostalCode with format and regExp
startOfWeekYesThe first day of the week in the country.Weekday.monday
regionalBlocsNoThe regional blocs of the country.[BlocEU()]
translationsYes1The 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 3166 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.

You can also find many common methods you may know from Dart ecosystem - toString overrides, copyWith, toJson, compareTo, etc. Also, a compile-time const, tree-shakable, case-insensitive code maps (for O(1)-time code look-ups), list, and much more.

[!TIP] 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".

  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

How do I get all countries using a specific currency?

  • Quick/simple way (more suitable for a single currency): .where

    final usdCountries = WorldCountry.list.where( // All countries using USD.
      (country) => country.currencies?.contains(const FiatUsd()) ?? false,
    );
    
  • Advanced (more suitable for multiple currencies, sorted by population): .byCountryMap()

    final map = const [FiatUsd(), FiatEur()].byCountryMap(); // All countries using USD or EUR.
    final allCountries = {for (final list in map.values) ...list};
    
  • 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.