Home > Enterprise >  how to cache firebase images
how to cache firebase images

Time:03-11

    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"];
  }
}

here is how the image is displayed in the ui code

NetworkImage(PocketBooks['image']),

I have this class witch fetch data from firestore along with images in firestore storage, i want o cache the images after they load in the app, how can i do that

CodePudding user response:

You can use the cached_network_image package which you can get from pub.dev

Once you imported the package on you widget you can use a widget like this to cache the image

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),
          );
        },
      )
  • Related