handle image errors when image doesn't exist in flutter

Viewed 2155

I am trying to load an image with FadeInImage, but when the image path does not exist, an AssetImage is loaded. However, the NetworkImage throws me error: HTTP request failed, statusCode: 404

I tried to do it with AdvancedNetworkImage and it works fine, but when the image is replaced by another, AdvancedNetworkImage keeps loading the old image, I guess it's the cache. I have disabled the options associated with cache, but it continues loading the previous image.

FadeInImage photoItem = FadeInImage(
        image: AdvancedNetworkImage('$urlapi/api/items/$itemID/images', 
            fallbackAssetImage: 'assets/images/add_photo.png',
            disableMemoryCache: true, 
            cacheRule: CacheRule(maxAge: Duration(seconds: 1)),
            timeoutDuration: Duration(seconds: 1),
            useDiskCache: false, ),
        placeholder: AssetImage('assets/images/add_photo.png'),
        fadeInDuration: Duration(milliseconds: 150),
        fit: BoxFit.contain,
      );

Any ideas how this could be handled?

2 Answers

The method is simple, use existing alternative images "placeholderErrorBuilder/imageErrorBuilder" and trying code as below

  Container(
padding: const EdgeInsets.only(left: 4),
child: FadeInImage(
    fadeInDuration: const Duration(milliseconds: 1000),
    image: CachedNetworkImageProvider(
     '$urlapi/api/items/$itemID/images',
      errorListener: () {},
    ),
    height: 80,
    width: 80,
    fit: BoxFit.fill,
    imageErrorBuilder: (context, error, stackTrace) =>
        SvgPicture.asset(
          "assets/images/example.svg",
          fit: BoxFit.fill,
          height: 70,
          //  color: Colors.white,
        ),
    placeholderErrorBuilder: (context, error, stackTrace) =>
        SvgPicture.asset(
          "assets/images/sliderph.svg",
          fit: BoxFit.fill,
          height: 70,
          //  color: Colors.white,
        ),
    placeholder: AssetImage('assets/images/add_photo.png')),
), 
Related