A photo costs memory by its pixels, not by its file size. An 800 KB photo from a phone camera can take 48 MB of memory the moment Flutter draws it. Shocked?
It's because a JPEG or PNG file is compressed, like a zip. To draw it, Flutter unpacks it into raw pixels, and every pixel takes 4 bytes of memory. A 4000×3000 camera photo unpacks to 4000 times 3000 times 4 bytes.
So, Flutter unpacks the photo at its full size, even when you show it tiny. A user's avatar, uploaded from their camera, shown in a small circle in a chat list, still unpacks to the full 48 MB (of memory). The circle needs maybe a few dozen KB of pixels. The other 47.9 MB is paid for and never seen.
cacheWidth is the fix:
Image.network( url, cacheWidth: 360,)
How to pick the right number? The sizes you write in Flutter are logical pixels, and a modern phone draws 2 or 3 real pixels for each one. So an avatar with width: 120 on a 3x phone is painted with 360 real pixels. That's the number the unpacker needs:
final int cacheWidth = (120 * MediaQuery.devicePixelRatioOf(context)).round();
Too low makes the image blurry, too high wastes memory. When unsure, round up. A little wasted memory is invisible, a blurry image is not.
One thing cacheWidth doesn't fix is the download. The full file still travels over the network, only the unpacking gets smaller. Making the download smaller is the server's job, by sending a thumbnail-sized image in the first place.
So the rule. Every network image you show at a known size gets a cacheWidth. The one exception is a photo the user can zoom into, there you want every pixel the file has.
Flutter can show you the waste in your own app. Turn this on:
Every image that unpacked much bigger than its spot on screen now renders upside down with inverted colors. Scroll through your app once and the wasteful ones scream at you.
One last piece. cacheWidth is a shortcut, under the hood it wraps the image in ResizeImage. Knowing that, you can add it to any image provider, including the disk-cached one from Cache network images. Here's the SmartNetworkImage from that article, upgraded. Pass a width and it unpacks at exactly that size, pass nothing and it unpacks in full:
import 'package:extended_image/extended_image.dart';import 'package:flutter/material.dart';import 'package:image_fade/image_fade.dart';class SmartNetworkImage extends StatelessWidget { const SmartNetworkImage( this.url, { super.key, this.width, this.height, this.fit = BoxFit.cover, }); final String url; final double? width; final double? height; final BoxFit fit; @override Widget build(BuildContext context) { ImageProvider image = ExtendedNetworkImageProvider( url, cache: true, ); // unpack at the size it's shown, not the size it was uploaded if (width != null) { final double dpr = MediaQuery.devicePixelRatioOf(context); image = ResizeImage( image, width: (width! * dpr).round(), ); } return ImageFade( image: image, width: width, height: height, fit: fit, placeholder: Container( color: Colors.grey.shade100, ), errorBuilder: (context, exception) { return Container( color: Colors.grey.shade100, child: Icon( Icons.broken_image_outlined, size: 20, color: Colors.grey.shade400, ), ); }, ); }}