sealed_currencies 1.5.1 sealed_currencies: ^1.5.1 copied to clipboard
Provides data for world currencies in the form of sealed classes.
// ignore_for_file: avoid_print, prefer-match-file-name
import "package:sealed_currencies/sealed_currencies.dart";
void main() {
print(FiatCurrency.list.length); // Prints: "169".
final serbianDinar = FiatCurrency.fromCode("RSD");
print(serbianDinar.name); // Prints: "Serbian Dinar".
final maybeEuro = FiatCurrency.maybeFromValue(
"Euro",
where: (currency) => currency.namesNative.first,
);
print(maybeEuro?.isEur); // Prints: true.
print(maybeEuro?.toString(short: false));
/*
Prints: "FiatCurrency(code: "EUR", name: "Euro", decimalMark: ",",
thousandsSeparator: ".", symbol: r"€", htmlEntity: "€", codeNumeric: "978",
namesNative: ["Euro"], priority: 2, smallestDenomination: 1, subunit: "Cent",
subunitToUnit: 100, unitFirst: true), translations: eurCurrencyTranslations".
*/
print(isVikingKrone(const FiatNok())); // Prints "true".
print(isVikingKrone(serbianDinar)); // Prints "null".
print(isVikingKrone(const FiatCzk())); // Prints "false".
FiatCurrency.list
.where((currency) => currency.symbol?.contains("kr") ?? false)
.forEach(print);
// Prints:
// Currency(code: DKK)
// Currency(code: ISK)
// Currency(code: NOK)
// Currency(code: SEK).
serbianDinar.toJson(); // Converts currency to a valid JSON.
// Prints German translations of all available regular currencies.
for (final currency in FiatCurrency.list) {
print(
"German name of ${currency.name}: "
"${currency.maybeTranslation(const BasicLocale(LangDeu()))?.name}",
);
}
print(const _FiatCustom().name); // Prints "Custom".
}
/// [Currency] is a sealed class, it means
/// this `whenOrNull` can be used same way as switch:
/// ```dart
/// switch (currency) {
/// case FiatDkk():
/// case FiatIsk():
/// case FiatNok():
/// case FiatSek():
/// return true;
/// case FiatCzk():
/// return false;
/// default:
/// return null;
///}
/// ```
// ignore: prefer-static-class, just for example.
bool? isVikingKrone(FiatCurrency currency) => currency.whenOrNull(
fiatCzk: () => false,
fiatDkk: () => true,
fiatIsk: () => true,
fiatNok: () => true,
fiatSek: () => true,
);
/// Creates a instance of the custom currency with permissive constructor.
class _FiatCustom extends FiatCurrency {
const _FiatCustom() : super.permissive(code: "123", name: "Custom");
}