Flutter 图片加载和缓存

Rah*_*hul 8 caching image cache-control flutter

这是我的用例:

  1. 我正在获取图像的 URL 列表。
  2. 我想在数据库中显示和缓存它们,不仅是针对特定会话,因此下次不应进行 Web 服务调用。

我们的应用程序也可以离线使用。我尝试了一些类似的库flutter_advanced_networkimageflutter_cache_manager但我遇到了相当大的延迟,而且大多数时候应用程序崩溃了。

Swi*_*ter 17

将其保存在您的应用程序的临时目录中:

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
  ...
}
Run Code Online (Sandbox Code Playgroud)

它将在应用程序启动时持续存在,并且只有在用户亲自清除缓存或重新安装应用程序时才会被删除。


Ali*_*i80 6

我一直在使用cached_network_image,效果如广告所示