metamonkey
metamonkey

Reputation: 447

Using an additional Future inside of a StreamBuilder with Flutter

I am trying to pull favicons dynamically and place them as a leading icon in ListTiles which make up a ListView, which is all contained inside a StreamBuilder. The StreamBuilder 'stream' variable is a FirebaseFirestore instance shown below:

streamBuilder = StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('users').doc(snapshot.data.uid).collection('vault').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> querySnapShot){

So the stream is updated every time the snapshot is updated. The snapshot contains docs which are iterated through in a for loop to populate each listtile in the listview. Each doc contains a url. I need to use that 'url' to retrieve that Listtile's respective favicon icon.

import 'package:favicon/favicon.dart' as iconz;

for (var doc in querySnapShot.data.docs) {
   String urlIcon = doc['urlIcon'];
   iconUse = iconz.Favicon.getBest(urlIcon);
leading: iconUse,
title:...

The problem is that the getBest() function returns a future. i.e. Using 'await' for my favicon getBest() function is not allowed inside the StreamBuilder, and my StreamBuilder is already dependent upon the snapshot stream. How do I use an additional Future inside of a StreamBuilder that is already dependent upon a different stream?

Upvotes: 1

Views: 286

Answers (1)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12353

You can use a FutureBuilder for leading: iconUse

import 'package:favicon/favicon.dart' as iconz;

for (var doc in querySnapShot.data.docs) {
   String urlIcon = doc['urlIcon'];
   //iconUse = iconz.Favicon.getBest(urlIcon);
   leading: FutureBuilder(
   future: iconz.Favicon.getBest(urlIcon),
   builder: (context, snapshot2) {
      if (snapshot2.hasData) {
          return snapshot2.data; //or whatever object you want to return from your function.
      } 
    ),

 title:...

Upvotes: 1

Related Questions