getInstanceFromLocale static method
Abstract base class for currency operations following ISO 4217 standards.
This class provides a common interface for all currency types and includes static methods to manage and retrieve currency instances according to ISO 4217 codes or locale information.
Use this class to:
- Retrieve currency instances by code or locale.
- Access currency metadata such as symbol, numeric code, or display name.
- Work with all available currencies in a consistent manner.
Example usage:
// Get a currency by ISO code
final usd = Currency.getInstance('USD');
// Get a currency by locale
final eur = Currency.getInstanceFromLocale(Locale('de', 'DE'));
// List all available currencies
final allCurrencies = Currency.getAllCurrencies();
// Access currency properties
print(usd.symbol); // $
print(eur.displayName); // Euro
Returns a Currency instance for the given Locale.
Determines the currency using the country code from the locale. Throws UnsupportedOperationException if the locale has no associated currency.
Example:
final usd = Currency.getInstanceFromLocale(Locale('en', 'US'));
final eur = Currency.getInstanceFromLocale(Locale('de', 'DE'));
Implementation
static Currency getInstanceFromLocale(Locale locale) {
if (locale.getCountry() == null) {
throw UnsupportedOperationException('Locale must have a country code to determine currency');
}
final currencyCode = CurrencyDatabase.getCurrencyCodeForLocale(locale.getCountry()!);
if (currencyCode == null) {
throw UnsupportedOperationException('No currency found for locale: ${locale.getLanguageTag()}');
}
return getInstance(currencyCode);
}