英文:
flutter web - download image from url, get Error: XMLHttpRequest error
问题
我想要使用 Firebase 存储的 URL 下载图像。
以下是我的代码
Future<void> _download(String _url) async {
print("开始下载");
final response = await http.get(Uri.parse(_url));
print("响应完成");
final imageName = path.basename(_url);
print(imageName);
final appDir = await path_provider.getApplicationDocumentsDirectory();
print(appDir);
final localPath = await path.join(appDir.path, imageName);
print(localPath);
final imageFile = File(localPath);
print("图像文件完成");
await imageFile.writeAsBytes(response.bodyBytes);
print("下载完成");
listDownloadPath.add(await localPath);
setState(() {});
print("下载结束");
}
在 Android 上运行正常,但在 Chrome 上尝试时出现错误。
有什么方法可以修复这个错误吗?谢谢。
英文:
i want to download image from firebase storage using the url.
here is my code
Future<void> _download(String _url) async {
print("START DOWNLOADDD");
final response = await http.get(Uri.parse(_url));
print("response done");
final imageName = path.basename(_url);
print(imageName);
final appDir = await path_provider.getApplicationDocumentsDirectory();
print(appDir);
final localPath = await path.join(appDir.path, imageName);
print(localPath);
final imageFile = File(localPath);
print("imageFile done");
await imageFile.writeAsBytes(response.bodyBytes);
print("download");
listDownloadPath.add(await localPath);
setState(() {});
print(await "END DOWNLOADDDD");
}
it works in android, but when i try it on chrome it get error when get the response.
is there any way that i can do to fix it? thank you
答案1
得分: 0
这是一个在部分代码中出现的问题:“http.get(Uri.parse(_url))”
我认为可以参考这个。
https://stackoverflow.com/questions/71157863/dart-flutter-http-request-raises-xmlhttprequest-error
-
删除 flutter_tools.stamp
1- 前往 flutter\bin\cache 并删除一个名为:flutter_tools.stamp 的文件 -
修改 chrome.dart
2- 前往 flutter\packages\flutter_tools\lib\src\web 并打开文件 chrome.dart。
3- 查找 '--disable-extensions'
4- 添加 '--disable-web-security'
=>
...
'--disable-extensions',
'--disable-web-security',
...
并使用 Uri.https('urlPath', 'callPath') 而不是 Uri.parse(url)
英文:
It is a problem that arises in part "http.get(Uri.parse(_url))"
I think it would be good to refer to this.
https://stackoverflow.com/questions/71157863/dart-flutter-http-request-raises-xmlhttprequest-error
-
Remove flutter_tools.stamp
1- Go to flutter\bin\cache and remove a file named: flutter_tools.stamp -
Modify chrome.dart
2- Go to flutter\packages\flutter_tools\lib\src\web and open the file chrome.dart.
3- Find '--disable-extensions'
4- Add '--disable-web-security'
=>
...
'--disable-extensions',
'--disable-web-security',
...
And use Uri.https('urlPath', 'callPath') instead of Uri.parse(url)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论