Flutter_cached_network_image: pre-download images & store them in cache

Created on 11 Oct 2018  路  4Comments  路  Source: Baseflow/flutter_cached_network_image

My App uses this plugin in order to display a scrollable list of images in a specific area.
There is, however, time between the download of the list and the actual display of an image, and some chance that the internet connection drops.
So I was looking for a method to pre-download the images and make sure they're in the right place for CacheNetworkImage to pick up.

A simple way could be to utilize CachedNetworkImage itself, but there was no ready method available. Here is a suggestion:

  downloadImage(String url) async {
    print("downloading $url");
    var provider = CachedNetworkImageProvider(url);
    var imageSub = StreamController<String>();
    final stream = imageSub.stream;
    provider.load(provider).addListener((image, yesNo) {
      print("image?: $image");
      if (image == null) {
        imageSub.sink.addError(Error());
      } else {
        imageSub.sink.add(url);
      }
      imageSub.sink.close();
    });
    return stream.single.whenComplete(() {
      imageSub?.close();
    });
  }

If this method is useful enough, I would be happy to prepare a PR for inclusion.

Most helpful comment

I am currently working on a seperation of the various functions of the cache_manager. I will keep in mind that this should be possible as well.

All 4 comments

I am currently working on a seperation of the various functions of the cache_manager. I will keep in mind that this should be possible as well.

You can now access the CacheManager and already download the file if you want.

@renefloor Would you mind providing a quick example about how to do this?

@jasonlfunk did you find out?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BerndWessels picture BerndWessels  路  6Comments

gunhansancar picture gunhansancar  路  3Comments

srburton picture srburton  路  6Comments

gregko picture gregko  路  6Comments

port3000 picture port3000  路  5Comments