The argument type 'Stream<User?>' can't be assigned to the parameter type 'Stream<User>?'

Viewed 1736

I wanted to use firebase authentication using Streams, however i am getting the above error on line stream: FirebaseAuth.instance.authStateChanges(),. I have tried onAuthStateChange() that is also not working.

class LandingPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Widget build(BuildContext context) {
      return StreamBuilder<User>(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (BuildContext context, AsyncSnapshot<User> snapshot) {
          if(snapshot.hasData) {
            print("data exists");
            return First();
          }
          else {
            return SignIn();
          }
        },
      );
    }
  }
}
1 Answers

If you look at the documentation for authStateChanges you'll see that it returns a Stream<User?>. So your StreamBuilder should also be of User? instead of User:

return StreamBuilder<User?>(
  ...
Related