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

outdated

Resumable http download request

example/download_task_example.dart

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

void main() async {
  final url = Uri.parse(
    "https://golang.org/dl/go1.19.1.src.tar.gz",
    // "https://not.found/html.txt",
  );

  double previousProgress = 0.0;
  final task = await DownloadTask.download(url,
    file: File("${Directory.current.path}/example/Go.tar.gz"),
  );

  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;
    }
  });

  // 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();
}
13
likes
0
points
11.4k
downloads

Publisher

verified publisherstarkdev.org

Weekly Downloads

Resumable http download request

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

http

More

Packages that depend on download_task