Flutter Image loading and caching
Here is my use case:
- I am getting the list of URLs of images.
- I want to display and cache them in the database, not only for a particular session so for the next time it should not do a web service call.
Our app is working offline as well. I tried a few libraries like flutter_advanced_networkimage
and flutter_cache_manager
but I'm getting quite lag and most of the times app crash.
Save it in your app's temp directory:
import 'dart:io';
// https://pub.dev/packages/path_provider
import 'package:path_provider/path_provider.dart';
final Directory temp = await getTemporaryDirectory();
final File imageFile = File('${temp.path}/images/someImageFile.png');
if (await imageFile.exists()) {
// Use the cached images if it exists
} else {
// Image doesn't exist in cache
await imageFile.create(recursive: true);
// Download the image and write to above file
...
}
It will persist through app launches and only gets deleted when the user personally clears the cache or reinstalls the app.
I've been using cached_network_image
, works as advertised