-
-
Save shinayser/14f20e3db91f1a04f9f2c9879ab678be to your computer and use it in GitHub Desktop.
Download file with progress in Dart/Flutter using 'http' package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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