downloadFile static method

Future<Uint8List?> downloadFile({
  1. required String url,
  2. required String cacheKey,
  3. required void onProgress(
    1. int progress
    ),
  4. Duration timeout = const Duration(seconds: 60),
})

Implementation

static Future<Uint8List?> downloadFile({
  required String url,
  required String cacheKey,
  required void Function(int progress) onProgress,
  Duration timeout = const Duration(seconds: 60),
}) async {
  if (_operations.containsKey(cacheKey)) {
    await _operations[cacheKey]?.cancel();
  }

  final CancelableOperation<Uint8List> op = CancelableOperation<Uint8List>.fromFuture(
    _performDownload(url: url, cacheKey: cacheKey, onProgress: onProgress, timeout: timeout),
    onCancel: () => _operations.remove(cacheKey),
  );

  _operations[cacheKey] = op;

  try {
    final Uint8List data = await op.value;
    _operations.remove(cacheKey);
    return data;
  } catch (e) {
    _operations.remove(cacheKey);
    rethrow;
  }
}