title: Not able to fetch particular user document by user id in flutter app
body: <p>I want to retrieve a particular user document by its id from the collection users. When I directly pass the particular user id, I get the data. But when I pass it using variable it shows null.</p>
  <p>My code is as follows:</p>
  <pre><code>import 'package:cloud_firestore/cloud_firestore.dart';
  import 'package:flutter/material.dart';
  import 'package:firebase_auth/firebase_auth.dart';
  import '../services/crud.dart';
  
  class test extends StatefulWidget {
    @override
    _testState createState() =&gt; _testState();
  
  }
  class _testState extends State&lt;test&gt; {
  
    String userID=&quot;&quot;;
    @override
    void initState() {
      super.initState();
      ///get current user and assign his id
      FirebaseAuth.instance.currentUser().then((FirebaseUser user) {
        setState(() {
          userID = user.uid;
          print(userID);
        });
      });
    }
    @override
    Widget build(BuildContext context) {
      return SingleChildScrollView(
        child: Column(
          children: &lt;Widget&gt;[
            StreamBuilder(
                    stream: Firestore.instance.collection('users').document(userID).snapshots(),
                    builder: (context,snapshot){
                      if (!snapshot.hasData) return const Text(&quot;Loading...&quot;);
                      else return Container(
                        child: Column(
                          children: &lt;Widget&gt;[
                            Text(snapshot.data[&quot;name&quot;]),
                            Text(snapshot.data[&quot;email&quot;]),
                            Text(snapshot.data[&quot;phone&quot;].toString()),
                          ],
                        ),
                      );
                    },
                  )
          ],
        )
      );
    }
  }
  
  </code></pre>
  <p>When I'm using the following line of code with specifying uid it shows the result :</p>
  <pre><code>stream: Firestore.instance.collection('users').document('d6DshRomJMkIe9mAARAi').snapshots(),
  </code></pre>
  <p>But it does not work when I pass <strong>userID</strong> inside the document(). Even though userID contains the actual id of the logged-in user.</p>
  <pre><code>stream: Firestore.instance.collection('users').document(userID).snapshots(),
  </code></pre>
  <p>The error says :</p>
  <pre><code>NoSuchMethodError: The method '[]' was called on null.
  Receiver: null
  Tried calling [](&quot;name&quot;)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/Ko8ce.png" rel="nofollow noreferrer">This is the structure of my database.</a></p>
  <p><a href="https://i.stack.imgur.com/eKizs.png" rel="nofollow noreferrer">This is the error that I get on my app screen.</a></p>