Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Image loading and caching

Here is my use case:

  1. I am getting the list of URLs of images.
  2. 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.

like image 619
Rahul Avatar asked May 14 '19 07:05

Rahul


2 Answers

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.

like image 145
Swift Avatar answered Nov 07 '22 15:11

Swift


I've been using cached_network_image, works as advertised

like image 30
Ali80 Avatar answered Nov 07 '22 14:11

Ali80