Flutter 颤振Dio包:如何收听下载进度从另一个类?
我有一个Flutter 颤振Dio包:如何收听下载进度从另一个类?,flutter,stream,progress,listen,dio,Flutter,Stream,Progress,Listen,Dio,我有一个downloadservice类,它使用dio包处理文件下载。我想听听我的ViewModel类的下载进度,该类在DownloadService类中实现了downloadsfile方法。我该怎么做 以下是我的下载服务类的代码片段: class DownloadsService { final String urlOfFileToDownload = 'http://justadummyurl.com/'; //in my actual app, this is user input
downloadservice
类,它使用dio
包处理文件下载。我想听听我的ViewModel
类的下载进度,该类在DownloadService
类中实现了downloadsfile
方法。我该怎么做
以下是我的下载服务
类的代码片段:
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
});
}
}
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.
*/
}
}
}
这是我的ViewModel
课程:
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
});
}
}
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.
*/
}
}
}
Dio文档提供了如何将响应类型转换为流/字节的示例。。但它没有给出任何下载文件时如何执行的示例。有人能给我指个正确的方向吗?我现在真的被困住了。。多谢各位 如果视图创建了视图模型,则必须在视图类中定义变量,然后将其传递给视图模型,并将其作为参数传递给下载服务 像这样:
class ViewModel {
PublishSubject publishSubject;
ViewModel(this.publishSubject);
DownloadsService _dlService = DownloadsService();
void implementDownload() {
if(Permission.storage.request().isGranted) {
_dlService.downloadFile(publishSubject);
}
}
}
因此,视图侦听在下载文件方法之前将发生的更改,该方法依次发送更改
像这样:
void downloadFile(PublishSubject publishSubject) {
Dio dio = Dio();
dio.download(urlOfFileToDownload, '$dir/$filename',
onReceiveProgress(received,total) {
int percentage = ((received / total) * 100).floor();
publishSubject.add(percentage);
});
}
class View {
PublishSubject publishSubject = PublishSubject();
ViewModel viewModel;
View(){
publishSubject.listen((value) {
// the value is percentage.
//can you refresh view or do anything
});
viewModel = ViewModel(publishSubject);
}
}
因此,接口将侦听以前发生的更改
像这样:
void downloadFile(PublishSubject publishSubject) {
Dio dio = Dio();
dio.download(urlOfFileToDownload, '$dir/$filename',
onReceiveProgress(received,total) {
int percentage = ((received / total) * 100).floor();
publishSubject.add(percentage);
});
}
class View {
PublishSubject publishSubject = PublishSubject();
ViewModel viewModel;
View(){
publishSubject.listen((value) {
// the value is percentage.
//can you refresh view or do anything
});
viewModel = ViewModel(publishSubject);
}
}
嘿,伙计,谢谢你的回复。我的视图类实际上只是我的UI代码。严格来说,我不想在视图类中放入任何业务逻辑。我想做的是,我的ViewModel类将侦听DownloadsService类,然后将当前值传递给View类。因此,我的View类所做的唯一一件事就是在UI中显示数据。不客气,我知道您真正想要的是什么,您可以在“ViewModel”中定义“publishSubject”变量并收听“DownloadsService”