Re: Check if URL is an image in Flutter

3,845 views
Skip to first unread message

Eric Seidel

unread,
Jun 11, 2018, 12:14:53 PM6/11/18
to leo nx, Flutter Dev

On Sun, Jun 10, 2018 at 11:42 AM leo nx <leo.d...@gmail.com> wrote:
I want to check if a URL returns a valid image and if it doesn't I want to show a different image. I used CachedNetworkImage, but it didn't work.

The problem is that if the image does not exist on the website, it redirects you to one of these 'Site not found', and i think that confuses the CachedNetworkImage, so the errorWidget is not displayed.

What i want the code to do is check if the first url works, and if not display the list view with the other 2 images.

Error in console:

    E/flutter ( 4808): [ERROR:flutter/lib/ui/painting/codec.cc(89)] Failed decoding image. Data is either invalid, or it is encoded using an unsupported format.
    I/flutter ( 4808): Another exception was thrown: Exception: operation failed

Code is as following:

    Widget _getImages(String url) {
    return new CachedNetworkImage(
      imageUrl: url,
      placeholder: new Center(child: new CircularProgressIndicator()),
      errorWidget: new ListView(
        children: <Widget>[
          new CachedNetworkImage(
            imageUrl: url.split('.')[0] + '-0.' + url.split('.')[1],
            placeholder: new Center(child: new CircularProgressIndicator()),
          ),
          new CachedNetworkImage(
            imageUrl: url.split('.')[0] + '-1.' + url.split('.')[1],
          )
        ],
      ),
    );
    }

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raman Plaha

unread,
Jan 19, 2021, 7:51:27 AM1/19/21
to Flutter Development (flutter-dev)
hi there, try this code, it worked for me 

child: Center(
child: Image.network(products[index].productImageList[0], fit: BoxFit.contain,
errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) {
return Image.asset("assets/defimg.jpg", fit: BoxFit.contain);
},
),
),
Reply all
Reply to author
Forward
0 new messages