fetch method

Future<PackageMetadata?> fetch({
  1. required String name,
  2. required Version currentVersion,
  3. bool fresh = false,
})

Fetches metadata for a single name/currentVersion pair.

Returns null when the package cannot be found on pub.dev. Serves from DiskCache when fresh is false and a valid entry exists.

Implementation

Future<PackageMetadata?> fetch({
  required String name,
  required Version currentVersion,
  bool fresh = false,
}) async {
  if (!fresh && _cache != null) {
    final hit = await _cache!.get('pub:$name');
    if (hit != null) return _parse(hit, currentVersion, fromCache: true);
  }

  final info = await _get('${Urls.pubDevApi}/packages/$name');
  if (info == null) return null;

  final score = await _get('${Urls.pubDevApi}/packages/$name/score');
  final blob = {'info': info, 'score': score};
  await _cache?.set('pub:$name', blob, ttl: const Duration(hours: 24));

  return _parse(blob, currentVersion, fromCache: false);
}