CachedNetworkImage image does not change with same name

Viewed 2434

I'm using https://pub.dev/packages/cached_network_image for display network image.

I came across with curious issue. Here I'm setting my profile image and url of profile image will be same, Only image will be changed.

Suppose if, First time I upload profile image then I will get url like "https://xx.yyy/user/profileAvatar" First time all works fine. But when I change profile image second time I will get same url as above with new image. But issue is here CachedNetworkImage can't able to load my new image.

Need your guidance to solve this issue.

PS. : I know, If I delete previous image from cache directory before uploading second one it will solve my issue.

UPDATED:

OMG, Same things even doesn't work with Image.network too.

4 Answers

If an image changes, I would consider that to be a new resource. If we consider it to be a new resource, then it should really have a new resource locator (i.e. new URI) associated with it. Caching images anywhere (in a browser, CDN, etc.) becomes pretty challenging if they are updating but using the same URI.

If the url is not changing for different images, its better to avoid using CachedNetworkImage... You can simply use NetworkImage widget

So for me calling MyCache.evict(key), deleting the image from disk and calling setState() did nothing. But if I also call MyImageDownloader.evict(key) before calling setState() that will trigger a new download which should get the new image for the same key.

The thing is that you have to evict and/or delete the image from everywhere including the class that is extending ImageProvider which in my case is MyImageDownloader.

If everything else is done and still you are not receiving the new image, then call the "not recommended" PaintingBinding.instance.imageCache.evict(key)

There are a couple of libraries out there and all have their own special ingredients. So try to experiment with the one that is working better for you. The idea is that you evict from any of the libraries' cache, delete from the disk, and call evict from whatever class is extending the ImageProvider.

Use this code when you want clear CachedNetworkImage

await CachedNetworkImage.evictFromCache(url);
Related