Flutter 如何在Flatter中将https网络URL图像下载到应用程序目录
将“package:http/http.dart”导入为http 导入“包:image\u picker\u saver/image\u picker\u saver.dart”Flutter 如何在Flatter中将https网络URL图像下载到应用程序目录,flutter,https,dart-pub,Flutter,Https,Dart Pub,将“package:http/http.dart”导入为http 导入“包:image\u picker\u saver/image\u picker\u saver.dart” try { var response = await http.get(imageURL); // problem is here (only working on http URL) debugPrint(response.statusCode.toString()); var filePath =
try {
var response = await http.get(imageURL); // problem is here (only working on http URL)
debugPrint(response.statusCode.toString());
var filePath = await ImagePickerSaver.saveFile(
fileData: response.bodyBytes);
var savedFile= File.fromUri(Uri.file(filePath));
print('Image path: $savedFile');
/* setState(() {
_imageFile = Future<File>.sync(() => savedFile);
});*/
} on PlatformException catch (error) {
print(error);
}
试试看{
var response=await http.get(imageURL);//问题在这里(仅处理http URL)
debugPrint(response.statusCode.toString());
var filePath=wait ImagePickerSaver.saveFile(
fileData:response.bodyBytes);
var savedFile=File.fromUri(Uri.File(filePath));
打印('图像路径:$savedFile');
/*设置状态(){
_imageFile=Future.sync(()=>savedFile);
});*/
}平台上异常捕获(错误){
打印(错误);
}
它看起来像这样:
以下是我当时给出的答案:
您可能想尝试一下这个包,它是一个支持文件下载的http客户机,并将其本地保存到给定路径
下面是一个代码示例(源代码:)
未来下载文件(字符串url)异步{
Dio Dio=Dio();
试一试{
var dir=等待getApplicationDocumentsDirectory();
等待dio.download(url,${dir.path}/myFile.txt),onProgress:(rec,total){
打印(“记录:$Rec,总计:$Total”);
});
}捕获(e){
印刷品(e);
}
打印(“下载完成”);
}
您还需要使用getApplicationDocumentsDirectory()