getCurrencyByCode static method

Map<String, dynamic> getCurrencyByCode(
  1. String code
)

Retrieves a currency based on its ISO 4217 code.

This static method searches through the list of available currencies and returns the corresponding ISO4217Currency object for the given code. If no currency is found with the provided code, null is returned.

Example:

var currency = ISO4217Currency.getCurrencyByCode('USD');
print(currency); // Output: ISO4217Currency(code: USD, numCode: 840, digits: 2, currencyName: United States Dollar, locations: [United States, American Samoa, British Virgin Islands])
  • code: The ISO 4217 3-letter currency code (e.g., 'USD').
  • Returns: An ISO4217Currency object if found, or null if the currency does not exist.

Implementation

static Map<String, dynamic> getCurrencyByCode(String code) {
  final currencies = ISO4217CurrencyData.currencyData();
  try {
    final currency = currencies
        .firstWhere((ISO4217Currency currency) => currency.code == code);
    return currency.toJson();
  } catch (e) {
    return {};
  }
}