forex_currency_conversion_historical

A Dart package to fetch FREE foreign exchange (forex) rates, both historical and current.

Features

  • Completely free — No API keys required
  • Historical rates — Rates dating back to January 1999
  • Wide currency support — 150+ currencies via multiple data sources
  • Safe numbers — Uses decimal package to safeguard against floating-point errors
  • Automatic fallback — Leverages multiple data sources in case one fails for any reason

Data Sources

This package intelligently combines multiple free forex APIs:

Source Date Range Currencies Notes
Currency API 2024-03-02 → Present 150+ Primary source, extensive coverage
Frankfurter 1999-01-04 → Present ~30 European Central Bank

The package automatically selects the appropriate source based on the requested date and currency pair.

Getting Started

Installation

Add to your pubspec.yaml:

dependencies:
  forex_currency_conversion_historical: ^1.0.0

Then run:

dart pub get

Usage

Fetch Today's Rate

import 'package:forex_currency_conversion_historical/forex_currency_conversion_historical.dart';

void main() async {
  final rate = await fetchForex('USD', 'EUR');
  print('1 USD = $rate EUR');
}

Fetch Historical Rates

final rate = await fetchForex('GBP', 'JPY', date: '2023-06-15');
print('GBP/JPY on 2023-06-15: $rate');

If you don't have a string representation of the date, you can pass in your DateTime variable and the package will convert it to a YYYY-MM-DD string for you. In this case use fetchForexParseDate.

Safely avoid Precision Errors (Floating point Errors)

This package returns decimal instead of double to ensure precision in financial calculations. As such you should avoid double at all costs!

Here is an illustration of the problem:

// With double (problematic)
double rate = 0.1 + 0.2; // = 0.30000000000000004 ❌

// With Decimal (precise)
Decimal rate = Decimal.parse('0.1') + Decimal.parse('0.2'); // = 0.3 ✅

License

Apache 2.0 LICENSE