Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload a FileImage in Flutter

Tags:

flutter

dart

Let's say I have an image on the device's disk and load it on the screen providing its path to a FileImage.

I edit that image and save it on the same path expecting calling the setState(() {}) function will reload it. But it doesn't.

I tried clearing the image cache by calling imageCache.clear() function and also imageProvider.evict() but no difference.

If I close that page and open it again, I see the updated images.

I assume the images that are being displayed on the screen are in the memory, if my assumption is correct, how to reload it?

like image 386
Javid Noutash Avatar asked Oct 02 '18 02:10

Javid Noutash


People also ask

How do you refresh a network image in Flutter?

Another tricky solution is to add a dummy argument which changes every time, then the image will be treat as different image source and will refresh image every time when you access it. For example add t=currentTimestamp, but you don't need handle this argument in the web server.

How do I use FileImage in Flutter?

In Flutter, displaying an image can be done by using Image widget. Flutter provides a named constructor File. Image which can be used if the image source is from a file. Alternatively, you can also use FileImage .

How do I clear cached image in Flutter?

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

How do I load an image from URL Flutter?

In Flutter, you can display an image from different possible sources. To display an image from URL, you may use Image.network() constructor of Image class.


2 Answers

I know I'm a bit late, but I used this workaround:

_tmpImageFile.existsSync() ? Image.memory(
  Uint8List.fromList(_tmpImageFile.readAsBytesSync()),
  alignment: Alignment.center,
  height: 200,
  width: 200,
  fit: BoxFit.contain,
) : Container(),
like image 167
Igor Kharakhordin Avatar answered Sep 23 '22 14:09

Igor Kharakhordin


This is also working for me:

Image previewImage = Image.file(
    File(scenePreviewFilename),
);

And when pressing a button or something just evict the cached image from imageCache like this:

setState(() {
    imageCache.evict(previewImage.image, includeLive: true);
});

PS: I had some issues getting this to work(similar to the initial post) because I was generating the image in another thread and the image was loaded before it was generated, resulting in the imageCache appearing not to have an effect ... so make sure you are not in this race situation.

like image 27
Cristi Avatar answered Sep 25 '22 14:09

Cristi