Beginner
Caching Images in Flutter: Using cached_network_image
· 6 min read
The Problem with Image.network
Every time Image.network(url) is used (say, when a list rebuilds, or the user leaves a page and comes back), Flutter's own engine does cache the image in memory, but that cache isn't persistent — it's cleared when the app restarts or under memory pressure. The result: downloading the same image over and over, wasting data and adding a brief loading delay each time.
How cached_network_image Works
The cached_network_image package stores every image it downloads both in memory and on the device's disk. On the next request, it first checks the disk copy; if it exists and hasn't expired, it's shown without ever hitting the network. For content that gets displayed repeatedly — product lists, avatars — this dramatically cuts both data usage and perceived load time.
Basic Usage
CachedNetworkImage(
imageUrl: urun.gorselUrl,
placeholder: (context, url) => const Center(
child: CircularProgressIndicator(strokeWidth: 2),
),
errorWidget: (context, url, error) => const Icon(Icons.broken_image),
fit: BoxFit.cover,
)placeholder sets the widget shown while the image loads; errorWidget sets the widget shown if the download fails. Both are optional, but for a good user experience you should almost always add them — otherwise an empty area shows up.
Custom Wrapping with imageBuilder
If instead of showing the image directly you want to, say, clip it into a rounded ClipRRect or put a gradient over it, use the imageBuilder parameter. It's called once the image is fully ready and gives you an ImageProvider:
CachedNetworkImage(
imageUrl: urun.gorselUrl,
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
image: DecorationImage(image: imageProvider, fit: BoxFit.cover),
),
),
placeholder: (context, url) => const CircularProgressIndicator(),
)Customizing Cache Management
The default cache behavior is fine for most apps, but you may want images to be re-downloaded after a certain period, or want to cap the cache size. For that, define your own CacheManager and pass it via the cacheManager parameter:
final ozelCacheManager = CacheManager(
Config(
'urunGorselleri',
stalePeriod: const Duration(days: 7),
maxNrOfCacheObjects: 200,
),
);
CachedNetworkImage(
imageUrl: urun.gorselUrl,
cacheManager: ozelCacheManager,
)Things to Watch Out For
• Don't forget the placeholder; without it, an empty area shows up until the image loads, and users may think something's broken. • For a large number of small images (like avatars), you can reduce RAM usage by limiting the in-memory resolution with the memCacheWidth/memCacheHeight parameters. • For user-specific images that change often (like a profile photo update), it's safe to add a version/timestamp parameter to the URL to avoid showing a stale cached copy — cached_network_image uses the URL itself as the cache key.
Image.network or cached_network_image?
For a one-off image shown rarely, Image.network is enough and needs no extra dependency. But when the same image appears in a list, a grid, or gets revisited repeatedly by the user (a product catalog, a news feed, avatars), cached_network_image noticeably improves both data usage and perceived performance.
Conclusion
cached_network_image eliminates repeated downloads by caching network images on disk and in memory. Build a smooth loading experience with placeholder and errorWidget, wrap the image however you like with imageBuilder, and take control of cache duration/size with your own CacheManager when needed.