title: How to efficiently access a firestore reference field's data in flutter?
body: <p>Using similar code as <a href="https://github.com/flutter/plugins/blob/master/packages/cloud_firestore/example/lib/main.dart" rel="noreferrer">flutter's firestore example</a>, suppose there is a reference field stored in a snapshot document, called: <code>document['userRef']</code>.</p>
  
  <p>First of all, how do I access the data of userRef? Using <code>document['userRef'].get().data</code> or <code>document['userRef'].get().username</code> I wasn't able to access the data. (<code>NoSuchMethodError: Class 'Future&lt;DocumentSnapshot&gt;' has no instance getter 'data'</code>)</p>
  
  <p>I also tried using <code>document['userRef'].get().then(...)</code> but getting the error: <code>type 'Future&lt;dynamic&gt;' is not a subtype of type 'String'</code></p>
  
  <p>Even if <code>.then</code> would work, wouldn't it then look up the same reference again for each message? Here the database is updated in realtime, but it's unnecessary to make the same lookup for multiple messages in the ListView.</p>
  
  <pre><code>class MessageList extends StatelessWidget {
    MessageList({this.firestore});
  
    final Firestore firestore;
  
    @override
    Widget build(BuildContext context) {
      return StreamBuilder&lt;QuerySnapshot&gt;(
        stream: firestore.collection('messages').snapshots(),
        builder: (BuildContext context, AsyncSnapshot&lt;QuerySnapshot&gt; snapshot) {
          if (!snapshot.hasData) return const Text('Loading...');
          final int messageCount = snapshot.data.documents.length;
          return ListView.builder(
            itemCount: messageCount,
            itemBuilder: (_, int index) {
              final DocumentSnapshot document = snapshot.data.documents[index];
              // document['userRef'] exists here
              return ListTile(
                title: Text(document['message'] ?? '&lt;No message retrieved&gt;'),
                subtitle: Text('Message ${index + 1} of $messageCount'),
              );
            },
          );
        },
      );
    }
  }
  </code></pre>
  
  <p>Edit:
  I was able to fetch the nested data using FutureBuilder, though not sure how efficient it is. (Wouldn't this possibly send loads of redundant requests to Firebase?)</p>
  
  <p>Creating a widget for the nested data, where document['userRef'] exists:</p>
  
  <pre><code>        FutureBuilder(
            future: userData(document['userRef']),
            builder: (BuildContext context,
                AsyncSnapshot&lt;dynamic&gt; uData) {
              return Text(uData.data['username']);
            },
          );
  </code></pre>
  
  <p>And the userData function looks like this:</p>
  
  <pre><code>Future&lt;dynamic&gt; userData(DocumentReference user) async {
    DocumentSnapshot userRef = await user.get();
    return userRef.data;
  }
  </code></pre>