Baseflow / flutter_cached_network_image

Download, cache and show images in a flutter app
https://baseflow.com
2.44k stars 655 forks source link

Dynamic S3 Buckket URL Caching #428

Open Dhananjayan-PN opened 4 years ago

Dhananjayan-PN commented 4 years ago

Hi, I would like to cache a Network Image from a S3 bucket URL against an id or key. I tried to use the key: parameter but it didn't work. Please advise how I can achieve this. Heres the Cached Network Image code with the 'key' parameter that doesn't seem to do what I intend.

        CachedNetworkImage(
           key: Key(uni['university_name'].toString()), // Doesn't seem to do the job
            imageUrl: uni['image_url'],
            placeholder: (context, url) => CardPlaceHolder(),
            errorWidget: (context, url, error) => Padding(
              padding: EdgeInsets.all(21),
              child: Icon(
                Icons.error,
                size: 30,
                color: Colors.red.withOpacity(0.8),
              ),
            ),
            imageBuilder: (context, imageProvider) => ...
brunorf commented 4 years ago

I'm trying to accomplish exactly this, and by coincidence I'm trying this right now. Revising the flutter_cached_network_image source code and flutter_cache_manager (used by the former) it seems the key parameter used by flutter_cache_manager as a lookup for cached files is not used anywhere bu cached_network_image. If I'm not missing anything, probably it'll be need to change CachedNetworkImageProvider to accept a lookup key and _loadAsync method to accept the same variable to be passed to Cache Manager (DefaultCacheManager) getFileStream method. I could put a pull request, but I don't know how to do that.

Alternatively, you could just extract the base url of your image and attach an http header with the token. This way your url for the same file will never change, so the CachedNetworkImage will work as expected.

Dhananjayan-PN commented 4 years ago

Thanks for the tip! Do let me know if a fix is pushed.