download static method

Future<void> download({
  1. required String url,
  2. required String targetPath,
  3. String? token,
  4. int maxRetries = 10,
  5. CancelToken? cancelToken,
})

Downloads a file with smart retry logic and HTTP-aware error handling

url - File URL (any server) targetPath - Local file path to save to token - Optional authorization token (e.g., HuggingFace, custom auth) maxRetries - Maximum number of retry attempts for transient errors (default: 10) cancelToken - Optional token for cancellation Note: Auth errors (401/403/404) fail after 1 attempt, regardless of maxRetries. Only network errors and server errors (5xx) will be retried up to maxRetries times.

This method waits for completion without progress tracking. For progress tracking, use downloadWithProgress instead.

Throws DownloadCancelledException if cancelled via cancelToken.

Implementation

static Future<void> download({
  required String url,
  required String targetPath,
  String? token,
  int maxRetries = 10,
  CancelToken? cancelToken,
}) async {
  final completer = Completer<void>();

  // Use downloadWithProgress but just wait for completion
  downloadWithProgress(
    url: url,
    targetPath: targetPath,
    token: token,
    maxRetries: maxRetries,
    cancelToken: cancelToken,
  ).listen(
    (_) {}, // Ignore progress updates
    onError: (error) => completer.completeError(error),
    onDone: () => completer.complete(),
    cancelOnError: true,
  );

  return completer.future;
}