I have used Image.memory(base64Decode(encodedString, fit: BoxFit.cover, width: imgWidth,height: imgHeight)
to load a base64 encoded image in flutter. In a listview viewing the images causes a flickering. Bcoz each time, the image is loaded again and again for each item in the list. So is there any way to cache a base64 encoded image in flutter?
Asked
Active
Viewed 1,748 times
1

Febin K R
- 886
- 2
- 11
- 21
-
try https://api.flutter.dev/flutter/painting/MemoryImage-class.html – pskink Dec 11 '19 at 11:41
-
@pskink Tried with `MemoryImage-class` still issue persists. – Febin K R Dec 11 '19 at 11:52
-
where do you call `MemoryImage` constructor? – pskink Dec 11 '19 at 11:55
-
@pskink Like this `Image(image: MemoryImage(base64Decode(encodedString)))` – Febin K R Dec 11 '19 at 12:00
-
1call it once, for example inside `State.initState` method – pskink Dec 11 '19 at 12:02
-
@pskink The `encodedString` is different for each item in the list. The images are different for almost every items in the list. So calling it only once won't work. – Febin K R Dec 11 '19 at 12:07
-
1`List
listOfImages = ...` – pskink Dec 11 '19 at 12:08 -
Could u please explain @pskink? – Febin K R Dec 11 '19 at 12:09
-
2use a list, of `MemoryImage`, not one `MemoryImage` - that list will store several `MemoryImage` each one made from different base64 string – pskink Dec 11 '19 at 12:10
-
or even better use `Map`, not `List` – pskink Dec 11 '19 at 12:17
-
@pskink Thanks a lott.. It worked.. If u can answer to this post it would be helpful to others too. – Febin K R Dec 11 '19 at 13:16
-
good, go ahead and write a self answer... – pskink Dec 11 '19 at 13:18
1 Answers
0
Instead of calling MemoryImage inside Widget build()
, you can initialize the base64 images inside initState()
.
late List<MemoryImage> _listImages;
@override
void initState(){
super.initState();
// load images here
_listImages.add(MemoryImage(imageData));
}
This should prevent the "flickering" happening on the widgets since the Screen is usually refreshed when the state has been changed.

Omatt
- 8,564
- 2
- 42
- 144