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?
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.
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();
}
}
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