Flutter Downloader

A plugin for creating and managing download tasks. Supports Aurora.

Aurora OS integration

Important: The plugin supports version 5.1.6 and higher

You should place this function in main.dart:

@pragma('vm:entry-point')
void requestEnqueue() async {
  WidgetsFlutterBinding.ensureInitialized();
  const backgroundChannel = MethodChannel('vn.hunghd/downloader_enqueue');
  await backgroundChannel.invokeMethod<void>('requestEnqueue');
}

This function is needed to start the background worker process. Worker is designed to download files in the background, for example, when the device is turned off or the application is minimized. For information on how to track download progress, see the "Usage -> For Aurora OS" section.

Usage

Import and initialize

import 'package:flutter_downloader/flutter_downloader.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  // Plugin must be initialized before using
  await FlutterDownloader.initialize(
    debug: true, // optional: set to false to disable printing logs to console (default: true)
    ignoreSsl: true // option: set to false to disable working with http links (default: false)
  );

  runApp(/*...*/)
}

Create new download task

The directory must be created in advance. After that, you need to provide the path of the directory in the savedDir parameter.

final taskId = await FlutterDownloader.enqueue(
  url: 'your download link',
  headers: {}, // optional: header send with url (auth token etc)
  savedDir: 'the path of directory where you want to save downloaded files',
  showNotification: true, // show download progress in status bar (for Android)
  openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);

Update download progress

void updateTaskState(String taskId, int status, int progress) {
  print(
    'task ($taskId) is in status ($status) and process ($progress)',
  );
}

void setupProgressCallback() {
  FlutterDownloader.registerCallback(updateTaskState, step: 1);
}

Load all download tasks

final List<DownloadTask>? tasks = await FlutterDownloader.loadTasks();

Load download tasks using a raw SQL query

final List<DownloadTask>? tasks = await FlutterDownloader.loadTasksWithRawQuery(query: query);

In order to parse data into DownloadTask object successfully, you should load data with all fields from the database (in the other words, use SELECT * ). For example:

SELECT * FROM task WHERE status=3

Below is the schema of the task table where flutter_downloader plugin stores information about download tasks

CREATE TABLE `task` (
  `id`  INTEGER PRIMARY KEY AUTOINCREMENT,
  `task_id` VARCHAR ( 256 ),
  `url` TEXT,
  `status`  INTEGER DEFAULT 0,
  `progress`  INTEGER DEFAULT 0,
  `file_name` TEXT,
  `saved_dir` TEXT,
  `resumable` TINYINT DEFAULT 0,
  `headers` TEXT,
  `show_notification` TINYINT DEFAULT 0,
  `open_file_from_notification` TINYINT DEFAULT 0,
  `time_created`  INTEGER DEFAULT 0
);

Cancel a task

FlutterDownloader.cancel(taskId: taskId);

Cancel all tasks

FlutterDownloader.cancelAll();

Pause a task

FlutterDownloader.pause(taskId: taskId);

Resume a task

FlutterDownloader.resume(taskId: taskId);

resume() will return a new taskId corresponding to a new background task that is created to continue the download process. You should replace the old taskId (that has paused status) by the new taskId to continue tracking the download progress.

Retry a failed task

FlutterDownloader.retry(taskId: taskId);

retry() will return a new taskId (just like resume())

Remove a task

FlutterDownloader.remove(taskId: taskId, shouldDeleteContent:false);

Open and preview a downloaded file

FlutterDownloader.open(taskId: taskId);