I have a DownloadsService
class that handles downloading of file using dio
package. I want to listen to the download progress from my ViewModel
class that implements the downloadFile
method inside my DownloadService
class. How do I do this?
Here's my code snippet for DownloadsService
class:
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
});
}
}
and this is my ViewModel
class:
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.
*/
}
}
}
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!