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?
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.
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 .
Go to Tools > Flutter > Flutter Clean to clear the build cache of the Flutter project.
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.
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(),
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With