title: flutter firebase streambuilder gives returned null error
body: <p>I am trying to read data realtime from my firebase firestore database through flutter. I learned that the best way to manage this is streambuilder. Checked a few sites on this topic and made a code like below and put a print statement just to see if everything works ok, however when i run the program it give the fallowing errors:</p>
  <pre><code>A build function returned null.
  The relevant error-causing widget was: 
    StreamBuilder&lt;DocumentSnapshot&gt; file:///C:/Users/faruk/Desktop/3-sayiavi/sayi_avi/lib/gamescreen.dart:116:15
  
  The method '[]' was called on null.
  Receiver: null
  Tried calling: [](&quot;status&quot;)
  The relevant error-causing widget was: 
    StreamBuilder&lt;DocumentSnapshot&gt; file:///C:/Users/faruk/Desktop/3-sayiavi/sayi_avi/lib/gamescreen.dart:116:15
  </code></pre>
  <p>My Code is below:</p>
  <pre><code>    import 'package:flutter/material.dart';
  import 'package:assets_audio_player/assets_audio_player.dart';
  import 'numbers.dart';
  import 'package:firebase_auth/firebase_auth.dart';
  import 'package:cloud_firestore/cloud_firestore.dart';
  
  
  Numbers myNumbers = Numbers();
  
  void main(){
    runApp(
        GameScreen()
    );
  }
  class GameScreen extends StatefulWidget {
    static String id ='gamescreen';
    @override
    _GameScreenState createState() =&gt; _GameScreenState();
  }
  
  class _GameScreenState extends State&lt;GameScreen&gt; {
    final _auth =FirebaseAuth.instance;
    FirebaseUser loggedInUser;
    final _firestore = Firestore.instance;
    String collectionPath = 'users';
    String docPath;
  
    @override
    void initState(){
      super.initState();
      getCurrentUser();
    }
    
    void getCurrentUser() async{
      try{
        final user = await _auth.currentUser();
        if(user !=null){
          loggedInUser =user;
          docPath = loggedInUser.uid;
          print(docPath);
          print(collectionPath);
        }
      }catch(e){
        print(e);
      }
    }
  
  
    Expanded attachNumber(imagenumber){
      return Expanded(
        child:FlatButton(
          onPressed: (){
            setState(() {
              if(!myNumbers.numberStatus[1]){
                myNumbers.buttonValues['numberimage1'] = imagenumber;
                myNumbers.numberStatus[1] =true;
              }else if(!myNumbers.numberStatus[2]){
                myNumbers.buttonValues['numberimage2'] = imagenumber;
                myNumbers.numberStatus[2] =true;
              }else if(!myNumbers.numberStatus[3]){
                myNumbers.buttonValues['numberimage3'] = imagenumber;
                myNumbers.numberStatus[3] =true;
              }else if(!myNumbers.numberStatus[4]){
                myNumbers.buttonValues['numberimage4'] = imagenumber;
                myNumbers.numberStatus[4] =true;
              }
            });
            final assetsAudioPlayer = AssetsAudioPlayer();
            assetsAudioPlayer.open(
              Audio(&quot;assets/audios/click.wav&quot;),
            );
          },
          padding: EdgeInsets.all(0),
          child: Image.asset('images/$imagenumber'),
        ),
      );
    }
  
    FlatButton showDeleteNumbers(statusNumber,number){
      return FlatButton(
        onPressed: (){
          setState(() {
            myNumbers.numberStatus[statusNumber] =false;
            myNumbers.buttonValues[number] = 'nonumber.png';
          });
        },
        child: Image.asset('images/'+myNumbers.buttonValues['$number']),
        //todo: _auth.signOut(); ekleyerek signout yapacaz
        //Navigator.pop(context); bir önceki ekrana dönecez;
      );
    }
  
  
  
  
  
    @override
    Widget build(BuildContext context) {
  
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.amberAccent,
            title: Text('Sayı Avı Oyun Ekranı'),
          ),
          body: StreamBuilder(
            stream: _firestore.collection(collectionPath).document(docPath).snapshots(),
            builder: (context,snapshot){
              if(snapshot.hasData){
                var userDocument = snapshot.data;
                print(userDocument['status']);
                return Column(
                  children: &lt;Widget&gt;[
                    Expanded(
                      flex: 80,
                      child: Row(
                        children: &lt;Widget&gt;[
                          Expanded(
                            flex: 50,
                            child: Column(
                              children: myNumbers.getUserNumbers(),
                            ),
                          ),
                          Expanded(
                            flex: 50,
                            child: Column(
                              children: myNumbers.getOpponentNumbers(),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      flex:10,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: &lt;Widget&gt;[
                          showDeleteNumbers(1,'numberimage1'),
                          showDeleteNumbers(2,'numberimage2'),
                          showDeleteNumbers(3,'numberimage3'),
                          showDeleteNumbers(4,'numberimage4'),
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 10,
                      child: Row(
                        children: &lt;Widget&gt;[
                          attachNumber('one.png'),
                          attachNumber('two.png'),
                          attachNumber('three.png'),
                          attachNumber('four.png'),
                          attachNumber('five.png'),
                          attachNumber('six.png'),
                          attachNumber('seven.png'),
                          attachNumber('eight.png'),
                          attachNumber('nine.png'),
                          attachNumber('zero.png'),
                        ],
                      ),
                    ),
                  ],
                );
              }
            },
          ),
        ),
      );
    }
  }
  </code></pre>
  <p>changed the code like this:</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:assets_audio_player/assets_audio_player.dart';
  import 'numbers.dart';
  import 'package:firebase_auth/firebase_auth.dart';
  import 'package:cloud_firestore/cloud_firestore.dart';
  
  
  Numbers myNumbers = Numbers();
  
  void main(){
    runApp(
        GameScreen()
    );
  }
  class GameScreen extends StatefulWidget {
    static String id ='gamescreen';
    @override
    _GameScreenState createState() =&gt; _GameScreenState();
  }
  
  class _GameScreenState extends State&lt;GameScreen&gt; {
    final _auth =FirebaseAuth.instance;
    FirebaseUser loggedInUser;
    final _firestore = Firestore.instance;
    String collectionPath = 'users';
    String docPath;
  
    @override
    void initState(){
      super.initState();
      getCurrentUser();
    }
  
    void getCurrentUser() async{
      try{
        final user = await _auth.currentUser();
        if(user !=null){
          loggedInUser =user;
          docPath = loggedInUser.uid;
          print(docPath);
          print(collectionPath);
        }
      }catch(e){
        print(e);
      }
    }
  
  
    Expanded attachNumber(imagenumber){
      return Expanded(
        child:FlatButton(
          onPressed: (){
            setState(() {
              if(!myNumbers.numberStatus[1]){
                myNumbers.buttonValues['numberimage1'] = imagenumber;
                myNumbers.numberStatus[1] =true;
              }else if(!myNumbers.numberStatus[2]){
                myNumbers.buttonValues['numberimage2'] = imagenumber;
                myNumbers.numberStatus[2] =true;
              }else if(!myNumbers.numberStatus[3]){
                myNumbers.buttonValues['numberimage3'] = imagenumber;
                myNumbers.numberStatus[3] =true;
              }else if(!myNumbers.numberStatus[4]){
                myNumbers.buttonValues['numberimage4'] = imagenumber;
                myNumbers.numberStatus[4] =true;
              }
            });
            final assetsAudioPlayer = AssetsAudioPlayer();
            assetsAudioPlayer.open(
              Audio(&quot;assets/audios/click.wav&quot;),
            );
          },
          padding: EdgeInsets.all(0),
          child: Image.asset('images/$imagenumber'),
        ),
      );
    }
  
    FlatButton showDeleteNumbers(statusNumber,number){
      return FlatButton(
        onPressed: (){
          setState(() {
            myNumbers.numberStatus[statusNumber] =false;
            myNumbers.buttonValues[number] = 'nonumber.png';
          });
        },
        child: Image.asset('images/'+myNumbers.buttonValues['$number']),
        //todo: _auth.signOut(); ekleyerek signout yapacaz
        //Navigator.pop(context); bir önceki ekrana dönecez;
      );
    }
  
  
  
  
  
    @override
    Widget build(BuildContext context) {
  
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.amberAccent,
            title: Text('Sayı Avı Oyun Ekranı'),
          ),
          body: StreamBuilder&lt;DocumentSnapshot&gt;(
            stream: _firestore.collection(collectionPath).document(docPath).snapshots(),
            builder: (BuildContext context,AsyncSnapshot&lt;DocumentSnapshot&gt; snapshot){
              if (snapshot.hasError) {
                return Text('Something went wrong');
              }
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Text(&quot;Loading&quot;);
              }
              if(snapshot.hasData){
                Map&lt;String, dynamic&gt; userDocument  = snapshot.data.data;
                print(userDocument);
                return Column(
                  children: &lt;Widget&gt;[
                    Expanded(
                      flex: 80,
                      child: Row(
                        children: &lt;Widget&gt;[
                          Expanded(
                            flex: 50,
                            child: Column(
                              children: myNumbers.getUserNumbers(),
                            ),
                          ),
                          Expanded(
                            flex: 50,
                            child: Column(
                              children: myNumbers.getOpponentNumbers(),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      flex:10,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: &lt;Widget&gt;[
                          showDeleteNumbers(1,'numberimage1'),
                          showDeleteNumbers(2,'numberimage2'),
                          showDeleteNumbers(3,'numberimage3'),
                          showDeleteNumbers(4,'numberimage4'),
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 10,
                      child: Row(
                        children: &lt;Widget&gt;[
                          attachNumber('one.png'),
                          attachNumber('two.png'),
                          attachNumber('three.png'),
                          attachNumber('four.png'),
                          attachNumber('five.png'),
                          attachNumber('six.png'),
                          attachNumber('seven.png'),
                          attachNumber('eight.png'),
                          attachNumber('nine.png'),
                          attachNumber('zero.png'),
                        ],
                      ),
                    ),
                  ],
                );
              }
            },
          ),
        ),
      );
    }
  }
  </code></pre>
  <p>Currently it gets the values of the map when i click save button, but when i go back and forward in the avd it gets null again. What may be wrong?</p>
  <p>what i would like to do is, there is a firebase function which matches users according to their &quot;status&quot; in the database. when a user enters this screen, its status will be &quot;on&quot; in the database and when firebase function finds another user with &quot;on&quot; status it will make both their status &quot;ready&quot; or something like this. When the user's status changed to &quot;ready&quot; instead of showing a for example &quot;searching for a match&quot; screen it will change to game screen and 2 ppl will start to play turn based. So to sum up as soon as user enters this screen it should start to listen changes in its status. Also as it will be a turn based game it should continue to listen other fields later for database changes. I hope i could have explained myself.</p>
  <p>Updated the flutter firebase code for new dependencies below</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:assets_audio_player/assets_audio_player.dart';
  import 'numbers.dart';
  import 'package:firebase_auth/firebase_auth.dart';
  import 'package:cloud_firestore/cloud_firestore.dart';
  
  
  Numbers myNumbers = Numbers();
  
  void main(){
    runApp(
        GameScreen()
    );
  }
  class GameScreen extends StatefulWidget {
    static String id ='gamescreen';
    @override
    _GameScreenState createState() =&gt; _GameScreenState();
  }
  
  class _GameScreenState extends State&lt;GameScreen&gt; {
    final _auth =FirebaseAuth.instance;
    User loggedInUser;
    final _firestore = FirebaseFirestore.instance;
    final String collectionPath = 'users';
    String docPath;
    DocumentReference userdoc;
  
    @override
    void initState(){
      super.initState();
      getCurrentUser();
    }
  
  
    void getCurrentUser() async{
      try{
        final user = await _auth.currentUser;
        if(user !=null){
          loggedInUser =user;
          docPath = loggedInUser.uid;
        }
      }catch(e){
        print(e);
      }
    }
  
  
    Expanded attachNumber(imagenumber){
      return Expanded(
        child:FlatButton(
          onPressed: (){
            setState(() {
              if(!myNumbers.numberStatus[1]){
                myNumbers.buttonValues['numberimage1'] = imagenumber;
                myNumbers.numberStatus[1] =true;
              }else if(!myNumbers.numberStatus[2]){
                myNumbers.buttonValues['numberimage2'] = imagenumber;
                myNumbers.numberStatus[2] =true;
              }else if(!myNumbers.numberStatus[3]){
                myNumbers.buttonValues['numberimage3'] = imagenumber;
                myNumbers.numberStatus[3] =true;
              }else if(!myNumbers.numberStatus[4]){
                myNumbers.buttonValues['numberimage4'] = imagenumber;
                myNumbers.numberStatus[4] =true;
              }
            });
            final assetsAudioPlayer = AssetsAudioPlayer();
            assetsAudioPlayer.open(
              Audio(&quot;assets/audios/click.wav&quot;),
            );
          },
          padding: EdgeInsets.all(0),
          child: Image.asset('images/$imagenumber'),
        ),
      );
    }
  
    FlatButton showDeleteNumbers(statusNumber,number){
      return FlatButton(
        onPressed: (){
          setState(() {
            myNumbers.numberStatus[statusNumber] =false;
            myNumbers.buttonValues[number] = 'nonumber.png';
          });
        },
        child: Image.asset('images/'+myNumbers.buttonValues['$number']),
      );
    }
  
  
  
  
    @override
    Widget build(BuildContext context) {
      userdoc = _firestore.collection(collectionPath).doc(docPath);
      return StreamBuilder&lt;DocumentSnapshot&gt;(
        stream: userdoc.snapshots(),
        builder: (BuildContext context, AsyncSnapshot&lt;DocumentSnapshot&gt; snapshot) {
          if (snapshot.hasError) {
            return Text('Something went wrong');
          }
  
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Text(&quot;Loading&quot;);
          }
          if(snapshot.hasData){
            Map&lt;String, dynamic&gt; userDocument  = snapshot.data.data();
            print(collectionPath);
            print(docPath);
            print(snapshot.data);
            print(userDocument);
            return MaterialApp(
              home:Scaffold(
                appBar: AppBar(
                  backgroundColor: Colors.amberAccent,
                  title: Text('Sayı Avı Oyun Ekranı'),
                ),
                body:Column(
                  children: &lt;Widget&gt;[
                    Expanded(
                      flex: 80,
                      child: Row(
                        children: &lt;Widget&gt;[
                          Expanded(
                            flex: 50,
                            child: Column(
                              children: myNumbers.getUserNumbers(),
                            ),
                          ),
                          Expanded(
                            flex: 50,
                            child: Column(
                              children: myNumbers.getOpponentNumbers(),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      flex:10,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: &lt;Widget&gt;[
                          showDeleteNumbers(1,'numberimage1'),
                          showDeleteNumbers(2,'numberimage2'),
                          showDeleteNumbers(3,'numberimage3'),
                          showDeleteNumbers(4,'numberimage4'),
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 10,
                      child: Row(
                        children: &lt;Widget&gt;[
                          attachNumber('one.png'),
                          attachNumber('two.png'),
                          attachNumber('three.png'),
                          attachNumber('four.png'),
                          attachNumber('five.png'),
                          attachNumber('six.png'),
                          attachNumber('seven.png'),
                          attachNumber('eight.png'),
                          attachNumber('nine.png'),
                          attachNumber('zero.png'),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            );
          }
        },
      );
    }
  }
  </code></pre>
  <p>You can see there are 4 prints to check if i am getting the data correctly. Currently when i run the app it shows</p>
  <p>I/flutter (15703): users
  I/flutter (15703): viPK8SpL9MOIV2bQqGzBkKVdYAk2
  I/flutter (15703): Instance of 'DocumentSnapshot'
  I/flutter (15703): null</p>
  <p>The 4th one shouldnt supposed to be null, when i click &quot;save&quot; button in android studio i get</p>
  <p>I/flutter (15703): users
  I/flutter (15703): viPK8SpL9MOIV2bQqGzBkKVdYAk2
  I/flutter (15703): Instance of 'DocumentSnapshot'
  I/flutter (15703): {password: 123456, lose: 0, win: 0, email: faruk@durusoy.net, status: ready}</p>
  <p>Which is the correct result.</p>