How to avoid OOM using images in ListView, GridView builders in Flutter?

Issue

I am trying to lazy load Images in any kind of ListView, GridView, StaggeredGridView, SliverGrid, SliverList, ScrollablePositionedList builders.
I am fetching images from network using Image.network.

I am also using ResizeImage to reduce the memory taken according to devices screen sizes.

The images are put in cache (which is actually not desired) and never cleared from memory when calling dispose.

Image.memory is actually behaving the same way, keeping references to object and not deleting them when calling dispose.

Here is my related github issue.

How can i only keep in memory the visible items in the ListView ?

Solution

You can use :

PaintingBinding.instance!.imageCache!.maximumSizeBytes = 1024 * 1024 * 50; // 50MB

Which cap the size retained in memory.

Answered By – nicover

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *