0

Here's my database (screenshot)

So I can display all books to the user in a pretty standard way using listview:

StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance.collection('books').snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError)
          return new Text('Error: ${snapshot.error}');
        switch (snapshot.connectionState) {
          case ConnectionState.waiting: return new Text('Loading...');
          default:
            return new ListView(
              children: snapshot.data.documents.map((DocumentSnapshot document) {
                return new ListTile(
                  title: new Text(document['text']),
                  subtitle: // I want to display the author's name
                );
              }).toList(),
            );
        }
      },
    );

But the problem is, document['author'] is a DocumentReference, which I can get by DocumentSnapshot ds = await documentSnapshot['author'].get(). But the problem is, this is a future which I cannot use inside the ListView. What is the best way to go about this? Use a Futurebuilder for each document snapshot? I also have multiple document references for each book in my actual project

Evan
  • 870
  • 1
  • 12
  • 24

1 Answers1

0

You might want to use a FutureBuilder instead of a StreamBuilder. According to this answer it also removes some builder plate code. Let me know how this unfolds. You can always return a ListView.builder if your snapshot does not contain any errors. That's my approach.

flarkmarup
  • 5,129
  • 3
  • 24
  • 25