If your ListView
in Flutter is caching images (e.g., with the fast_cached_network_image
package), and you’re observing that image fields do not update even though other fields in the list items do, it’s likely due to caching behavior or widget tree reuse. Here are a few possible causes and how to address them:
1. Image URL is the same
fast_cached_network_image
and other similar libraries use the image URL as the cache key. If the URL doesn’t change, the widget assumes the image hasn’t changed either.
Solution:
If the image content changes but the URL stays the same (e.g., same URL, but the image was updated on the server), you can:
- Add a query parameter (cache buster) to the URL like:
'https://yourdomain.com/image.jpg?timestamp=${staticInstanceGeneratedOnceAtRuntime}'
Or, ideally, use a version number or hash that changes when the image changes:
'https://yourdomain.com/image.jpg?v=123'
2. Widget is being recycled with same key
If you’re using keys incorrectly (or not at all), Flutter may reuse widgets assuming nothing has changed.
Solution:
Use a unique Key
for each item in the list:
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return ListTile(
key: ValueKey(item.id),
leading: FastCachedImage(url: item.imageUrl),
title: Text(item.title),
);
},
)
3. The image cache is not invalidated
Sometimes, the library holds on to old images even if they are supposed to be replaced.
Solution:
You may need to manually evict an image from the cache when it updates:
FastCachedImageConfig.removeUrl(item.imageUrl); // Only if supported by the library
Or use a different image URL to force reload.
4. Immutable data model not used
If you’re mutating objects in-place (e.g., changing item.imageUrl
but the reference remains the same), Flutter won’t re-render unless the object identity changes.
Solution:
Use immutable data models and replace the entire item in your list when its content changes.