Home > Enterprise >  How to store CachedNetworkImage in the phone storage?
How to store CachedNetworkImage in the phone storage?

Time:05-17

I have CachedNetworkImage widgets and they seem to work fine storing the images while the app is open, but the moment the app closes the images have to be redownloaded and cached again.

On the phone, the app shows 0 data usage. How do I set this up? The images are simple fetches from some URLs currently.

CodePudding user response:

Image(image: CachedNetworkImageProvider(url))

The cached network images stores and retrieves files using the flutter_cache_manager.

CachedNetworkImageProvider has cacheManager argument, which can be used to customise behaviour to some extent.

CodePudding user response:

If you use Network Image instead you just need to wait for the image to load again and then grab the bytedata...

//For local images use this:
//final ByteData bytes =await rootBundle.load('assets/image.png');
//var avatar = bytes.buffer.asUint8List();
//Save contact("displayName","givenName",["[email protected]"],["0123456789"],avatar );

//For network images use this:
var networkImage = new NetworkImage("https://url/of/image.jpg");
networkImage .obtainKey(new ImageConfiguration()).then((val) {
  var load = networkImage .load(val);
  load.addListener((listener, err) async {
    var image = listener;
    var avatar = image.toByteData().buffer.asUInt8List();
    saveContact("displayName","givenName",["[email protected]"],["0123456789"],avatar );
  });
});

void saveContact(String displayName, String givenName, List emails, List phones, Uint8List avatar){
  Contact newContact = Contact(
     displayName: displayName,
     givenName: givenName,
     emails: emails,
     phones: phones,
     avatar: avatar,
 );
}
  • Related