Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shinayser
Forked from ajmaln/downloadFile.dart
Last active June 21, 2020 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinayser/14f20e3db91f1a04f9f2c9879ab678be to your computer and use it in GitHub Desktop.
Save shinayser/14f20e3db91f1a04f9f2c9879ab678be to your computer and use it in GitHub Desktop.
Download file with progress in Dart/Flutter using 'http' package
import 'dart:async';
import 'dart:io';
import 'package:http/http.dart' as http;
///Returns the file as a stream of bytes
Future<Stream<List<int>>> downloadAsStream(String url) async {
var result = await Client().send(
Request(
"get",
Uri.parse(url),
)..persistentConnection = false,
);
return result.stream;
}
///Returns the fully downloaded file
Future<List<int>> download(String url) async =>
(await downloadAsStream(url)).fold([], (prev, element) => prev + element);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment