sealed_countries 3.0.0 copy "sealed_countries: ^3.0.0" to clipboard
sealed_countries: ^3.0.0 copied to clipboard

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

example/lib/main.dart

// ignore_for_file: avoid_print, it's a test

import "package:sealed_countries/sealed_countries.dart";

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

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

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

  final europeanUnion = RegionalBloc.fromAcronym("EU");
  final europeButNotEU = europeanCountries.where(
    (cnt) => !(cnt.regionalBlocs?.contains(europeanUnion) ?? false),
  );

  print(europeButNotEU.length); // Prints 23 count of non-EU European countries.

  const customOne = CountryCustom(code: "XKX", continent: Asia()); // Custom.
  print(describeCountry(const CountryUsa())); // Prints: "United States".
  print(describeCountry(customOne)); // Prints: "Custom territory XKX".
  print(describeCountry(country)); // Prints: "Standard catalog entry".

  /// Prints all the countries in the world with their phone code.
  for (final cnt in WorldCountry.list) {
    print("${cnt.name.common} code: ${cnt.idd.phoneCode()}");
  }

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

  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}");
  }
}

// ignore: prefer-static-class, just an example.
String describeCountry(WorldCountry country) => switch (country) {
  CountryUsa() => country.name.common,
  CountryCustom(:final code) when code.startsWith("X") =>
    "Custom territory $code",
  WorldCountry(:final continent) when continent is Oceania =>
    "Oceania catalog entry",
  // ignore: avoid-wildcard-cases-with-sealed-classes, just an example.
  _ => "Standard catalog entry",
};
29
likes
160
points
5.44k
downloads
screenshot

Publisher

verified publishertsin.is

Weekly Downloads

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

Repository (GitHub)
View/report issues

Topics

#countries #country #land #iso-3166 #flags

Documentation

API reference

License

MIT (license)

Dependencies

l10n_countries, sealed_currencies

More

Packages that depend on sealed_countries