getCurrencyByNumCode static method

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

Retrieves a currency based on its ISO 4217 numeric code.

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

Example:

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

Implementation

static Map<String, dynamic> getCurrencyByNumCode(String code) {
  final currencies = ISO4217CurrencyData.currencyData();
  try {
    final numCode = int.tryParse(code) ?? -1;
    final currency = currencies.firstWhere(
      (ISO4217Currency currency) => currency.numCode == numCode,
      orElse: () => ISO4217Currency(
        code: '',
        numCode: numCode,
        digits: 0,
        currencyName: 'Undefined',
        locations: [''],
      ),
    );
    return currency.toJson();
  } catch (e) {
    return {};
  }
}