如何缓存 firebase 图像

how to cache firebase images

    import 'package:cloud_firestore/cloud_firestore.dart';

class PocketBooksModel {
  String? docId;
  String? title;
  String? author;
  String? image;

  PocketBooksModel({this.docId, this.title, this.author, this.image});

  PocketBooksModel.fromMap(DocumentSnapshot data) {
    docId = data.id;
    title = data["title"];
    author = data["author"];
    image = data["image"];
  }
}

下面是图像在 ui 代码中的显示方式

NetworkImage(PocketBooks['image']),

我有这个 class 从 firestore 中获取数据以及 firebase 存储中的图像。我想在图像加载到应用程序后缓存它们。我该怎么做?

您可以使用 cached_network_image 包,您可以从 pub.dev

获得

在小部件上导入包后,您可以使用这样的小部件来缓存图像

CachedNetworkImage(
        imageUrl: imageUrl ,
        imageBuilder: (context, imageProvider) =>
            CircleAvatar(backgroundImage: imageProvider),
        placeholder: (context, url) => const Center(
          child: 
              CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation(
                    primary,
                  ),
                ),
        ),
        errorWidget: (context, url, error) {
          return const Center(
            child: Icon(Icons.error, color: Colors.red),
          );
        },
      )