Flutter:Dio下载文件到本地
import 'dart:io';
import 'package:dio/dio.dart';
main(){
// 创建dio对象
final dio = Dio();
// 下载地址
var url = 'https://*******.org/files/1.0.0.apk';
// 手机端路径
String savePath = Directory.systemTemp.path+'/ceshi.apk';
print(savePath);
downLoad(dio,url,savePath);
}
downLoad(Dio dio,String url,String savePath){
dio.download(url, savePath,onReceiveProgress:onReceiveProgress).then((value){
print(value);
}).whenComplete((){
print('下载结束');
}).catchError((onError){
print(onError);
});
}
// 下载的进度
void onReceiveProgress(int count, int total) {
print('文件大小:$total 当前进度:$count');
if(total != -1){
print((count / total *100).toStringAsFixed(0)+'%');
}
}