buildModelDownloadTask function

  1. @visibleForTesting
DownloadTask buildModelDownloadTask({
  1. required String taskId,
  2. required String url,
  3. required String? token,
  4. required BaseDirectory baseDirectory,
  5. required String directory,
  6. required String filename,
  7. required bool allowPause,
})

Builds the DownloadTask for a model download.

Extracted so the shape can be asserted without a device — every field here is load-bearing and every one of them failed silently when wrong:

  • group must be SmartDownloader.downloadGroup, the SAME constant registerCallbacks uses. A drift between the two ends means the task lands in a group nobody is registered for, _emitStatusUpdate finds no callback and no updates listener, and the download hangs forever while background_downloader merely logs a warning (#383's class).
  • updates must include progress, or the download looks frozen at 0%.
  • retries: 0 because retries are handled here with HTTP-aware logic; package-level retries would race that loop.
  • priority — see SmartDownloader.downloadPriority.

Implementation

@visibleForTesting
DownloadTask buildModelDownloadTask({
  required String taskId,
  required String url,
  required String? token,
  required BaseDirectory baseDirectory,
  required String directory,
  required String filename,
  required bool allowPause,
}) => DownloadTask(
  taskId: taskId,
  url: url,
  group: SmartDownloader.downloadGroup,
  headers: {
    if (token != null) 'Authorization': 'Bearer $token',
    'Connection': 'keep-alive',
    // Attempt to work around CDN ETag issues
    'Cache-Control': 'no-cache, no-store',
    'Pragma': 'no-cache',
  },
  baseDirectory: baseDirectory,
  directory: directory,
  filename: filename,
  requiresWiFi: false,
  // Auto-detect: false for HuggingFace (weak ETags), true for others
  allowPause: allowPause,
  priority: SmartDownloader.downloadPriority,
  retries: 0,
  updates: Updates.statusAndProgress,
);