D
PerformanceMedium30 XP4 min read

How do you optimize image loading and caching in Flutter?

TL;DR: Use cached_network_image for automatic disk+memory caching. Always set cacheWidth/cacheHeight to decode at display size (not full resolution). Use precacheImage() for critical images. Use WebP/AVIF format for 30-50% smaller files. Enable ResizeImage for memory optimization.

Full Answer

Images are the #1 memory consumer in most Flutter apps. A 4K image decoded at full resolution uses 32MB of RAM โ€” even if displayed at 100x100dp.

Decoding at display size

cacheWidth/cacheHeight tell Flutter to decode the image at a specific pixel size, not the full resolution. A 3000x2000 photo displayed in a 200x133 thumbnail should be decoded at 200x133 (scaled for device DPR).

๐ŸŽฏ

ResizeImage wraps any ImageProvider and adds size constraints at the codec level. Use it with NetworkImage, AssetImage, and FileImage โ€” not just CachedNetworkImage.

Code Examples

dartImage optimization techniques
Output
// Full 3000x2000 image: 3000 ร— 2000 ร— 4 bytes = 24MB RAM
// ResizeImage(200, 200): 200 ร— 200 ร— 4 = 160KB RAM
// 99.3% memory reduction for a thumbnail!

Common Mistakes

  • โœ—Not setting cacheWidth/cacheHeight on thumbnails โ€” entire image decoded at original resolution
  • โœ—Using Image.network without caching โ€” re-downloads on every build

Interview Tip

๐Ÿ’ก

The cacheWidth/cacheHeight parameter is one of the highest-impact single-line performance improvements in Flutter. Many developers don't know it exists. Mentioning it shows real-world performance experience.

#images#caching#cached_network_image#memory#resolution