Home > OS >  Caching of "FileImage" in flutter
Caching of "FileImage" in flutter

Time:10-30

I am working on an app, which displays lots of images from the local storage to the user and I wonder what the best caching strategy would be.

I am using FileImage as the ImageProvier. Since I am displaying the same Image, at different places I fear that using FileImage multiple time would load the image multiple times unnecessarily.

On the other Hand, if I add a caching layer, I fear that I might run out of memory.

So my questions:

  • Does FileImage cache the image? Or does it reload if I call it again?
  • What is a good caching strategy? I have to somehow find out which images are not displayed/used anymore.

CodePudding user response:

  1. Yes it does, you may invoke the evict method to remove an image from cache then it will get the image from scratch again.
  2. That's up to the cache manager to handle expired or changed cached objects. You are using local storage so it should not be a major concern, flutter will be fast/smart enough to see whats already stored. And as @pskink already mentioned, flutter caches everything.

If you want full control, I would recommend using this flutter_cache_manager 3.1.2.

There is a method called get getFileFromCache. It only retrieves from cache and returns no file when the file is not in the cache.

Before making a decision though, I would highly recommend you run your application on a physical device in profile mode to analyse your performance first, that should give you a timeline and memory snapshots that you can dig into and see what Flutter is doing with your images and how much resources it is currently taking. Check this link to the official dev docs, in short don't fix what's not broken.

  • Related