1

I am iterating through a listview of objects and retrieving favicon URL's from package:favicon/favicon.dart as a future: in a FutureBuilder. I then use these URL's to return and cache the favicon images in containers as leading listtile icons with a CachedNetworkImageProvider.

This works great, however it takes time to load every time the listview is refreshed because it has to retrieve the best url from the favicon url getter.

How do I cache the future: value so that there is no delay in using the URL retriever after the first use. Here is the code:

return new ListTile(
  leading: 
  FutureBuilder(
  future: iconz.Favicon.getBest(urlBase),
    builder: (context, snapShot2) {
      if (snapShot2.hasData){
       String urlIcon = snapShot2.data.url.toString();
       var containImage = Container(
       height: 40,
       width: 40,
       decoration: BoxDecoration(
       shape: BoxShape.circle,
       image: DecorationImage(           
       image: CachedNetworkImageProvider(urlIcon),
       fit: BoxFit.fill
       ),
       ),
       );
       return containImage;
     } 
     else {
      return 
      const Icon(Icons.remove_red_eye, color: Color(0xff00caff), size: 40);
     }
    }

  ),

  title: ...

In summary: How to use flutter_cache_manager on future: iconz.Favicon.getBest(urlBase), ?

metamonkey
  • 427
  • 7
  • 33

1 Answers1

1

You can cache the data in a map stored in a place where state is persistent.

// Make sure this is declared somewhere stateful
final Map<String, String> _urls;

/// Retrieves and caches favicon data.
Future<String> _getBest(String urlBase) async () =>
    _urls[urlBase] ??= (await iconz.Favicon.getBest(urlBase)).url.toString();

// Now use `_getBest(urlBase)` as your `FutureBuilder` `Future`.
hacker1024
  • 3,186
  • 1
  • 11
  • 31