Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter converting _firebaseAuth.authStateChanges() for Null safety

I am attempting to convert an old flutter code to a null safety code and encounter a problem with an abstract authentication class using firebase, basically its listening to a authStateChange

abstract class AuthBase {
  User get currentUser;
  Stream<User> authStateChanges();
  ....
}

class Auth implements AuthBase {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

  @override
  Stream<User> authStateChanges() => _firebaseAuth.authStateChanges();

But after running the code it returns A value of type 'Stream<User?>' can't be returned from the method 'authStateChanges' because it has a return type of 'Stream<User>'. error
so what i did was cast it

@override
Stream<User> authStateChanges() => _firebaseAuth.authStateChanges() as Stream<User>;

But now i encounter a new problem type '_AsBroadcastStream<User?>' is not a subtype of type 'Stream<User>' in type cast any advice on how to address this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

_firebaseAuth.authStateChanges() 

will give User or null so need to make it nullable as

Stream<User?> authStateChanges() => _firebaseAuth.authStateChanges() as Stream<User?>;

and also need to change in the base class as well as same.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading