title: Flutter Dio Package: How to listen to download progress from another class?
body: <p>I have a <code>DownloadsService</code> class that handles downloading of file using <code>dio</code> package. I want to listen to the download progress from my <code>ViewModel</code> class that implements the <code>downloadFile</code> method inside my <code>DownloadService</code> class. How do I do this? </p>
  
  <p>Here's my code snippet for <code>DownloadsService</code> class:</p>
  
  <pre><code>class DownloadsService {
     final String urlOfFileToDownload = 'http://justadummyurl.com/'; //in my actual app, this is user input
     final String filename = 'dummyfile.jpg';
     final String dir = 'downloads/$filename'; //i'll have it saved inside internal storage downloads directory
  
     void downloadFile() {
       Dio dio = Dio();
       dio.download(urlOfFileToDownload, '$dir/$filename', onReceiveProgress(received, total) {
          int percentage = ((received / total) * 100).floor(); //this is what I want to listen to from my ViewModel class
       });
     }
  }
  </code></pre>
  
  <p>and this is my <code>ViewModel</code> class:</p>
  
  <pre><code>class ViewModel {
     DownloadsService _dlService = DownloadsService(); //note: I'm using get_it package for my services class to make a singleton instance. I just wrote it this way here for simplicity..
  
        void implementDownload() {
  
         if(Permission.storage.request().isGranted) { //so I can save the file in my internal storage
            _dlService.downloadFile();
  
          /*
           now this is where I'm stuck.. My ViewModel class is connected to my View - which displays
           the progress of my download in a LinearProgressIndicator. I need to listen to the changes in
           percentage inside this class.. Note: my View class has no access to DownloadsService class. 
         */
  
        }        
     }
  }
  </code></pre>
  
  <p>The Dio documentation provides an example on how to make the response type into a stream/byte.. But it doesn't give any example on how to do it when downloading a file. Can someone point me to a right direction? I'm really stuck at the moment.. Thank you very much!</p>