getRate method
Uses caching to avoid fetching the rate every time.
To set the cache duration see cacheDuration.
Implementation
Future<Rate> getRate({required Currency from, required Currency to}) async {
// Create a fake rate so we get a [_Cache] object with the right id.
final fakeCache = _Cache(Rate(from, to, 1.0));
var cache = _cache.lookup(fakeCache);
if (cache != null &&
cache.created.add(cacheDuration).isBefore(DateTime.now())) {
_cache.remove(cache);
cache = null;
}
if (cache == null) {
final latest = await this.latest(from: from);
_cache.addAll(latest.map((rate) => _Cache(rate)));
cache = _cache.lookup(fakeCache);
}
return cache!.rate;
}