Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetworkImage is caching the old image

Tags:

flutter

dart

I am using NetworkImage class to display an image from the internet, the following is the code

return new Container(
        width: width,
        height: height,
        decoration: new BoxDecoration(
          color: const Color(0xff7c94b6),
          image: new DecorationImage(
            image: NetworkImage(url, headers: {"Authorization": token}),

            fit: fit,
          ),
          borderRadius: new BorderRadius.all(new Radius.circular(150.0)),
          border: new Border.all(
            color: Color(AppColors.surfacePrimary.hex),
            width: 0.0,
          ),
        ),
      );

However the image is cached, and when the image is updated on the server, the application displays the old cached image. How can I stop the caching on NetworkImage ?

like image 768
Sami Kanafani Avatar asked Mar 12 '19 15:03

Sami Kanafani


People also ask

What is cached network image?

Cached network imageA flutter library to show images from the internet and keep them in the cache directory.

What is caching in Flutter?

A CacheManager to download and cache files in the cache directory of the app. Various settings on how long to keep a file can be changed. It uses the cache-control http header to efficiently retrieve files. The more basic usage is explained here.

How do I clear app cache programmatically in Flutter?

Go to Tools > Flutter > Flutter Clean to clear the build cache of the Flutter project.


1 Answers

You can evict the image loaded from an URL using

 void evictImage() {
   final NetworkImage provider = NetworkImage(url);
   provider.evict().then<void>((bool success) {
     if (success)
       debugPrint('removed image!');
   });
 }

or alternatively you can add a random query part to the URL

int counter = 0;
...
NetworkImage('https://example.com/images/image1.png?dummy=${counter++}');
like image 115
Günter Zöchbauer Avatar answered Dec 16 '22 02:12

Günter Zöchbauer