在Image.Network中找不到图像 [英] image not found in Image.Network

查看:113
本文介绍了在Image.Network中找不到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有ListTile,我在其中显示图像,但是如果我遇到404错误,如何更改图像的URL,在默认图像上,因为我得到404,因为图像中出现白屏,我尝试这样做:

in my app i have ListTile where i show my images but how can i change my images url if i have 404 error, on default image, cuz if i get 404 i have white screen in image, i trying this:

                String imageURL = 'someURL';
                String imageUrlDef = 'defaulURL';
                String url = imageUrlDef;

                Future checkImg() async{
                  var response = await http.get(imageURL);

                  if(response.statusCode == 200){
                    url = imageURL;
                  }
                }

推荐答案

您可以使用 CachedNetworkImage 库检查服务器上的图像是否可用,如果服务器上没有图像,则将您重定向到errorWidget,您可以根据需要在其中创建错误小部件,下面是它的示例,请检查它一次

You can use CachedNetworkImage library for the check Image availability on the server or not ,if there is image not available on server then ,it redirect you to the errorWidget where you can create your Error widget as you want , below is the example of it, please check it once

 CachedNetworkImage(
      imageUrl: "https://picsum.photos/250?image=9",
      placeholder: (context, url) => new CircularProgressIndicator(),
      errorWidget: (context, url, error) => new Icon(Icons.error),  //// YOU CAN CREATE YOUR OWN ERROR WIDGET HERE
                   )

这篇关于在Image.Network中找不到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆