In Flutter we are using CachedNetworkImage to display the images. This way they are also stored in caches to be displayed faster next time the item is displayed. While the image is downloaded we can display a placeholder.
If user is uploading a new image in the app it behaves the same. But since we already have the image locally (used for upload) it will be nicer to not display the placeholder anymore. My idea was to manually put the local image in cache so the CachedNetworkImage will directly use the local one instead of downloading it again.
To put the image in cache I used
final DefaultCacheManager cache = DefaultCacheManager();
final Uint8List bytes = await file.readAsBytes();
await cache.putFile(imageUrl, bytes);
Note: I have the imageUrl because this piece of code is run right after the upload is finished. It should be faster to add it immediately rather than downloading it completely.
The problem is CachedNetworkImage is not using the local cache, is alway downloading it.
Alexandru Popa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.