Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change or replace the ImageCache in Flutter?

Tags:

flutter

dart

I want to change the behavior of the ImageCache in my Flutter app. For example, I want to experiment with different eviction strategies. Or, I simply want zero caching (for experimenting).

How do I replace to change the ImageCache?

like image 652
Seth Ladd Avatar asked Dec 15 '17 16:12

Seth Ladd


2 Answers

Create a class that inherits from WidgetsFlutterBinding. Override createImageCache on that class to return the ImageCache implementation of your choice. Call the constructor on that class before you call runApp.


There's one binding per application. The first binding (subclass of BindingBase) that is created gets to be that binding. Calling runApp() instantiates the WidgetsFlutterBinding binding, which is usually what you want. One of the classes that WidgetsFlutterBinding mixes in, the PaintingBinding, introduces createImageCache as a way to let you override the image cache. It calls that method, and sets the global imageCache to whatever that method returns.

like image 115
Ian Hickson Avatar answered Sep 18 '22 09:09

Ian Hickson


Code snippet for Ian Hickson's proposal:

 import 'package:flutter/widgets.dart';

    class CustomImageCache extends WidgetsFlutterBinding {
      @override
      ImageCache createImageCache() {
        // Set your image cache size
        this.imageCache.maximumSize = 10;
        return super.createImageCache();
      }
    }
like image 39
Sasha Prokhorenko Avatar answered Sep 20 '22 09:09

Sasha Prokhorenko