Rahul
Rahul

Reputation: 332

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.

Upvotes: 12

Views: 19927

Answers (2)

Swift
Swift

Reputation: 3410

Save it in your app's temp directory using the path_provider package:

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.

Upvotes: 24

Ali80
Ali80

Reputation: 8706

I've been using cached_network_image, works as advertised

Upvotes: 6

Related Questions