download_task 1.0.1 copy "download_task: ^1.0.1" to clipboard
download_task: ^1.0.1 copied to clipboard

outdated

Resumable http download request - pause, resume, cancel, realtime progress and error handling

example/download_task_example.dart

import 'package:download_task/download_task.dart';
import 'dart:io' show File, Directory;

void main() async {
  // specify url and destionation
  final url = Uri.parse("https://golang.org/dl/go1.19.1.src.tar.gz");
  final file = File("${Directory.current.path}/example/Go.tar.gz");
  
  // initialize download request
  final task = await DownloadTask.download(url, file: file);

  // listen to state changes
  double previousProgress = 0.0;
  task.events.listen((event) { 
    switch (event.state) {
      case TaskState.downloading:
        final bytesReceived = event.bytesReceived!;
        final totalBytes = event.totalBytes!;
        if (totalBytes == -1) return;
        
        final progress = (bytesReceived / totalBytes * 100).floorToDouble();
        if (progress != previousProgress && progress % 10 == 0) {
          print("progress $progress%");
          previousProgress = progress;
        }
        break;
      case TaskState.paused:
        print("paused");
        break;
      case TaskState.success:
        print("downloaded");
        break;
      case TaskState.canceled:
        print("canceled");
        break;
      case TaskState.error:
        print("error: ${event.error!}");
        break;
    }
  });

  // control the process
  await Future.delayed(const Duration(milliseconds: 500));
  task.pause();
  await Future.delayed(const Duration(milliseconds: 500));
  task.resume();
  await Future.delayed(const Duration(milliseconds: 1500));
  task.cancel();
}
12
likes
0
pub points
80%
popularity

Publisher

verified publisherstarkdev.org

Resumable http download request - pause, resume, cancel, realtime progress and error handling

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

http

More

Packages that depend on download_task