简体   繁体   English

Flutter NetworkImage 处理 403 错误

[英]Flutter NetworkImage handle 403 error

In my Flutter mobile app while loading profile image of users through NetworkImage() , I am getting 403 status code in response.在我的Flutter移动应用程序中,通过NetworkImage()加载用户的个人资料图像时,我收到403状态代码作为响应。

How can I handle this by displaying an Image from my assets folder in case of 403 status code or if image URL is broken etc.在出现403状态代码或图像 URL 损坏等情况下,如何通过显示资产文件夹中的图像来处理此问题。

Currently I've handled it by sending a HTTP GET request to the image URL and checking if the status code is 200 .目前我已经通过向图像 URL 发送HTTP GET请求并检查状态代码是否为 200来处理它。 If it is 200 I use the NetworkImage() or else I use AssetImage() to load the image and use a FutureBuilder() to build the Widget.如果是 200,我使用NetworkImage() ,否则我使用AssetImage()加载图像并使用FutureBuilder()构建小部件。

While this works perfectly, I feel this a lot of trouble for handling such a small scenario and it can be done in a better way that I am unaware of.虽然这很有效,但我觉得处理这么小的场景很麻烦,而且可以以我不知道的更好的方式完成。

What is the best way to handle such scenarios?处理这种情况的最佳方法是什么?

You did it simple for this scenario, I didnt see any trouble:您在这种情况下做得很简单,我没有发现任何问题:

  if (response.statusCode == 200) {
    return NetworkImage();
  } else {
    return AssetImage();
  }

I achieved this by using precacheImage .我通过使用precacheImage实现了这一点。 This will precache your image to flutter cache.这会将您的图像预缓存到颤振缓存中。 It also has a onError function where you can handle errors while caching the image.它还有一个onError函数,你可以在缓存图像时处理错误。

String validatedUrl;

precacheImage(
  NetworkImage(urlToCheck),
  context,
  onError: (
    exception,
    stacktrace,
  ) {
    print(exception.toString());
    setState(() {
      validatedUrl = '';
    });
  },
).whenComplete(() {
  if (validatedUrl == null) {
    setState(() {
      validatedUrl = urlToCheck;
    });
  }
});

Then validatedUrl is either null, empty or carries the validated url.然后validatedUrl要么为null,要么为空,要么带有经过验证的url。

  • null -> not validated yet null -> 尚未验证
  • empty -> error while downloading image empty -> 下载图像时出错
  • url -> successfully downloaded image url -> 成功下载图片

Please try below approach.请尝试以下方法。 Here, If image is available, It will load network image else it will redirect to the error callback.在这里,如果图像可用,它将加载网络图像,否则它将重定向到错误回调。 In error callback you can return the widget you want.在错误回调中,您可以返回所需的小部件。 In your case, you want to load from asset so you can use it like as below.在您的情况下,您想从资产加载,以便您可以像下面这样使用它。 If you want to check error status code, you need to parse the exception that I have print.如果要检查错误状态代码,则需要解析我打印的异常。

 Image.network('https://www.outbrain.com/techblog/wp-content/uploads/2017/05/road-sign-361513_960_720.jpg',
            errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
            print("Exception >> ${exception.toString()}");           
              return Image.asset('assets/images/lake.jpg');
            },
          )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM