title: Flutter: StreamBuilder Snapshot -- No Data
body: <p>I am just learning Flutter and am trying to use a StreamBuilder to display a Login / Register page if the user is logged out, or a Profile page if the user is logged in. My code is below:</p>
  
  <p>Auth Service:</p>
  
  <pre><code>import 'package:firebase_auth/firebase_auth.dart';
  import 'package:flutter/material.dart';
  
  class AuthUser {
    AuthUser({@required this.uid, @required this.email});
    final String uid;
    final String email;
  }
  
  abstract class AuthBase {
    Future&lt;AuthUser&gt; currentUser();
    Future&lt;AuthUser&gt; signIn({String email, String pw});
    Future&lt;AuthUser&gt; registerUser({String email, String pw});
    Stream&lt;AuthUser&gt; get onAuthStateChanged;
    Future&lt;void&gt; signOut();
  }
  
  class Auth implements AuthBase {
    final _firebaseAuth = FirebaseAuth.instance;
  
    AuthUser _userFromFirebase(FirebaseUser user) {
      if (user != null) {
        return AuthUser(uid: user.uid, email: user.email);
      } else {
        return null;
      }
    }
  
    @override
    Stream&lt;AuthUser&gt; get onAuthStateChanged {
      return _firebaseAuth.onAuthStateChanged.map(_userFromFirebase);
    }
  
    @override
    Future&lt;AuthUser&gt; currentUser() async {
      final user = await _firebaseAuth.currentUser();
      return _userFromFirebase(user);
    }
  
    @override
    Future&lt;AuthUser&gt; signIn({String email, String pw}) async {
      final authResult = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: pw);
      return _userFromFirebase(authResult.user);
    }
  
    @override
    Future&lt;AuthUser&gt; registerUser({String email, String pw}) async {
      final authResult = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: pw);
      return _userFromFirebase(authResult.user);
    }
  
    @override
    Future&lt;void&gt; signOut() async {
      await _firebaseAuth.signOut();
    }
  }
  
  </code></pre>
  
  <p>StreamBuilder:</p>
  
  <pre><code>class WelcomeScreen extends StatelessWidget {
    WelcomeScreen({@required this.auth});
    static const String id = '/';
    final AuthBase auth;
  
    @override
    Widget build(BuildContext context) {
      return StreamBuilder&lt;AuthUser&gt;(
        stream: auth.onAuthStateChanged,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            AuthUser user = snapshot.data;
            if (user == null) {
              return displayLoginOrRegPage(context);
            } else {
              return ProjectScreen(
                user: user,
                auth: auth,
              );
            }
          } else {
            return Scaffold(
              body: Center(
                child: CircularProgressIndicator(),
              ),
            );
          }
        },
      );
    }
  </code></pre>
  
  <p>It was my understanding the stream would begin emitting 'null' once it was initialized, and would continue doing so until it fired off an Auth state change...</p>
  
  <p>But the snapshot continually reports "No Data" and thus my code is stuck on the CircularProgressIndicator.</p>
  
  <p>BTW, if I display the log-in screen in place of the progress indicator, the code works. So I'm clearly not understanding the whole stream initialization process.</p>
  
  <p>Can somebody kindly explain to me where I have gone wrong here? Thanks a million in advance.</p>