The right way to preload or precache images in Flutter for extreme fast rendering

The solution is quite simple. We need to pre-load the image before it is rendered. If it is preloaded and cached, the rendering of image when needed may seem instantaneous. Okay let’s see how can we implement Precaching in Flutter.

Here in this tutorial, we will learn the correct way to pre-cache a network image.

This is the code required for precaching an image. We use precacheImage() function for precaching an image. But the function can’t be called inside the initState(). If you try to call the function inside the initState(), it may throw an error “dependOnInheritedWidgetOfExactType< MediaQuery>() or dependOnInheritedElement() was called before _MyHomePageState.initState() completed”. So we call precacheImage(theImage.image, context) inside didChangeDependencies() which is called after the initState.

Scroll to Top