flutter web – download image from url, get Error: XMLHttpRequest error

huangapple go评论55阅读模式
英文:

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 上尝试时出现错误。

以下是错误信息
flutter web – download image from url, get Error: XMLHttpRequest error

有什么方法可以修复这个错误吗?谢谢。

英文:

i want to download image from firebase storage using the url.

here is my code

Future&lt;void&gt; _download(String _url) async {
    print(&quot;START DOWNLOADDD&quot;);
    final response = await http.get(Uri.parse(_url));
    print(&quot;response done&quot;);

    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(&quot;imageFile done&quot;);
    await imageFile.writeAsBytes(response.bodyBytes);
    print(&quot;download&quot;);

    listDownloadPath.add(await localPath);
    setState(() {});

    print(await &quot;END DOWNLOADDDD&quot;);

}

it works in android, but when i try it on chrome it get error when get the response.

here is the error
flutter web – download image from url, get Error: XMLHttpRequest error

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)

huangapple
  • 本文由 发表于 2023年2月24日 14:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定