Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading large pdf files in a Flutter app

Tags:

flutter

dart

I'm looking for a way to download large pdf files from an external server with a Flutter application for offline storage.

But downloading a large file (sometimes 100mb+) takes some time. I don't want the app being stuck in a wait function for it to download. What i'm looking for is a download function that has a callback with a progress report (Something like: 250000/500000 bytes done. Doesn't have to be exactly that. Just something that I can work with and make a progress bar out of).

Is this even possible to do in Flutter? The only things I came across were the HTTP library. But that does not seem to have a progress callback and just plainly reading the contents of a http call (Which also doesn't have a progress report). I hope someone has a method for me that I can use to make this happen.

Kind regards, Kevin Walter

EDIT: C# has the perfect example of what I mean
https://stackoverflow.com/a/9459441/2854656

like image 246
Kevin Walter Avatar asked Apr 15 '18 08:04

Kevin Walter


People also ask

How do I download a PDF from Flutter app?

We will create a PDF viewer page which will be a stateful widget, which will be responsible for viewing PDFs in our app. Also, we will create a saveFile() function to save the file locally in our internal storage, we will pass the URL of the PDF file and a name to save it as. Now, in main. dart inside the lib folder.


1 Answers

  • https://docs.flutter.io/flutter/dart-io/HttpClient-class.html
  • https://docs.flutter.io/flutter/dart-io/HttpClientResponse-class.html
int fileSize;
int downloadProgress = 0;

new HttpClient().get('localhost', 80, '/file.txt')
     .then((HttpClientRequest request) => request.close())
     .then((HttpClientResponse response) {
       fileSize ??= respone.contentLength;
       response.transform(utf8.decoder).listen((contents) {
         downloadProgres += contents.length;
         // handle data
       });
     });
like image 57
Günter Zöchbauer Avatar answered Oct 18 '22 02:10

Günter Zöchbauer