Home > Enterprise >  Flutter converting _firebaseAuth.authStateChanges() for Null safety
Flutter converting _firebaseAuth.authStateChanges() for Null safety

Time:03-11

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.

CodePudding user response:

_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.

  • Related