downloadWithProgress static method

Stream<int> downloadWithProgress({
  1. required String url,
  2. required String targetPath,
  3. String? token,
  4. int maxRetries = 10,
})

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) 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. Returns a stream of progress percentages (0-100)

Implementation

static Stream<int> downloadWithProgress({
  required String url,
  required String targetPath,
  String? token,
  int maxRetries = 10,
}) {
  final progress = StreamController<int>();
  StreamSubscription? currentListener;

  _downloadWithSmartRetry(
    url: url,
    targetPath: targetPath,
    token: token,
    maxRetries: maxRetries,
    progress: progress,
    currentAttempt: 1,
    currentListener: currentListener,
    onListenerCreated: (listener) {
      currentListener = listener;
    },
  );

  return progress.stream;
}