如何解决Dart CallBack无法在Dio内部下载
我正在尝试使用dio在flutter应用程序中下载文件。我创建了一个单独的类来处理下载,并使用回调来获取要在Circular Progress Indicator中使用的进度数据。 这是我的下载课程代码。
class AttachmentDownload {
CancelToken cancelToken = CancelToken();
double progress = -1;
Dio dio;
Function callBack;
AttachmentDownload() {
dio = new Dio();
}
Future<void> download(
{@required Attachment attachment,@required callBack}) async {
final path = await getLocalFilePath(attachment);
try {
await dio
.download(attachment.url,path,onReceiveProgress: showDownloadProgress,cancelToken: cancelToken)
.whenComplete(() {
callBack(this.progress);
_saveDownload(attachment);
}).catchError((e) => print(e));
} catch (e) {
print(e);
}
}
void showDownloadProgress(int count,int total) {
this.progress = count / total;
callBack(this.progress);
}
}
_saveDownload(Attachment attachment) async {
final path = await getLocalFilePath(attachment);
await ReviewDownloadsDB().add({
'id': attachment.id,'name': attachment.name,'url': attachment.url,'path': path
});
}
回调失败,并出现DioError [DioErrorType.DEFAULT]:NoSuchMethodError:方法“调用”在null上被调用。
但是,它在whenComplete内部成功。有人可以告诉我我做错了什么以及如何解决此问题。预先感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。