Sub topic 13
Image caching and memory use
This chapter assumes you know Dart classes, Flutter widgets, StatelessWidget, StatefulWidget, and pubspec.yaml. You should also have a Flutter project that runs on an emulator, device, or the web.
How Flutter caches images
Flutter uses an in-memory image cache. When an ImageProvider, such as NetworkImage or AssetImage, resolves an image, Flutter can keep the decoded image available for later widgets. A second widget that requests the same image can often display it without decoding the image again.
The cache improves scrolling and reduces repeated work. It also uses memory. A large photo may use much more memory after decoding than its compressed file size suggests. A 4000 by 3000 image contains 12 million pixels. With four bytes per pixel, the decoded bitmap can require about 48 MB before other overhead.
The cache is not a permanent disk store. The operating system can remove app memory, and a later request may download or decode the image again.
Decode images at a useful size
The display size and the decoded size are separate concerns. width and height control layout. They do not necessarily make the source image cheaper to decode.
Use cacheWidth and cacheHeight when a large source image is displayed in a smaller area. These values ask Flutter to decode and cache a smaller version.
import 'package:flutter/material.dart';
const imageUrl = 'https://picsum.photos/id/1015/2400/1600';
void main() {
runApp(const MaterialApp(home: PhotoPage()));
}
class PhotoPage extends StatefulWidget {
const PhotoPage({super.key});
@override
State<PhotoPage> createState() => _PhotoPageState();
}
class _PhotoPageState extends State<PhotoPage> {
late final ImageProvider<Object> imageProvider;
@override
void initState() {
super.initState();
imageProvider = ResizeImage(
NetworkImage(imageUrl),
width: 600,
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
precacheImage(imageProvider, context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Cached photo')),
body: Center(
child: Image(
image: imageProvider,
width: 300,
height: 180,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) =>
const Icon(Icons.broken_image, size: 48),
),
),
);
}
}ResizeImage makes the provider used by precacheImage match the provider used by Image. This matters because Flutter identifies cache entries by the image provider and its configuration.
The cacheWidth or ResizeImage width is commonly chosen near the image's physical display width. A high-density screen may need more pixels than the logical Flutter width. Do not reduce the decode size so far that the image looks blurry.
Warming and limiting the cache
precacheImage loads an image before it appears. Use it for a likely first-screen hero image or the next page in a short flow. Do not precache an entire gallery. That can move the memory cost earlier without reducing it.
For long lists, use thumbnails rather than full-resolution originals. Reuse stable URLs and providers where possible. Avoid creating many unique query strings for the same image, because each distinct provider configuration can create another cache entry.
Exercise
Exercise
A 300-pixel-wide card displays a 4000-pixel-wide photo. Which change usually reduces decoded memory more: setting width: 300, or setting a suitable cacheWidth or ResizeImage width?
Show answer
Setting cacheWidth or using ResizeImage reduces decoded memory. The width property mainly controls the widget's layout size.