I'm trying to implement a getter, but it is showing me this error on last line in the below code snippet.

The code is -

class AuthRepository extends BaseAuthRepository {

  final FirebaseFirestore _firebaseFirestore;
  final auth.FirebaseAuth _firebaseAuth;

  AuthRepository({
    FirebaseFirestore? firebaseFirestore,
    auth.FirebaseAuth? firebaseAuth,
  })  : _firebaseFirestore = firebaseFirestore ?? FirebaseFirestore.instance,
        _firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance;

  @override
  // TODO: implement user
  Stream<auth.User> get user => _firebaseAuth.userChanges();


Solution 1: Pompidou

change

 Stream<auth.User> get user => _firebaseAuth.userChanges(); 

to

Stream<auth.User> get user => _firebaseAuth.userChanges()!;


Solution 2: tomerpacific

As can be seen in the documentation,

The method returns an object of Stream<User?>, which means that it may be null.

Since you can't be sure of the contents of the type returned, instead of returning it, you can check if it is null.

I.E.

class AuthRepository extends BaseAuthRepository {

final FirebaseFirestore _firebaseFirestore;
final auth.FirebaseAuth _firebaseAuth;

AuthRepository({
  FirebaseFirestore? firebaseFirestore,
  auth.FirebaseAuth? firebaseAuth,
  })  : _firebaseFirestore = firebaseFirestore ?? FirebaseFirestore.instance,
    _firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance;

 @override
 // TODO: implement user
 Stream<auth.User> get user => 
   val userChanges = _firebaseAuth.userChanges();
   if (userChanges != null) return userChanges
   else //your logic


Solution 3: Hemal Moradiya

try this

Stream<auth.User?> get user => auth.userChanges(); 


Solution 4: Victor Eronmosele

_firebaseAuth.userChanges() returns User? which is nullable so you need to update your return type to be nullable by adding ? after auth.User like below:

  Stream<auth.User?> get user => _firebaseAuth.userChanges();


Solution 5: Francis Happy Atta Baffo

This it worked for me:

Stream<auth.User?> get user => _firebaseAuth.userChanges();

It sends out events when a user's credentials are linked or unlinked, as well as when the user's profile is updated. This Stream's goal is to listen to real-time changes to the user state (signed-in, signed-out, different user & token refresh) without having to manually perform reload and then return changes to your application.

Check the documentation for more information.