A value of type 'Stream<User?>' can't be returned from the function 'user' because it has a return type of 'Stream<User>' in flutter

1,285

Solution 1

try this

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

Solution 2

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

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

change

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

to

Stream<auth.User> get user => _firebaseAuth.userChanges()!;
Share:
1,285
Shreyansh Sharma
Author by

Shreyansh Sharma

Updated on December 30, 2022

Comments

  • Shreyansh Sharma
    Shreyansh Sharma over 1 year

    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();
    
  • Shreyansh Sharma
    Shreyansh Sharma almost 3 years
    No. it is again giving the same error. In your code snippet, return userChanges is giving the same error.
  • Shreyansh Sharma
    Shreyansh Sharma almost 3 years
    Actually I tried it, but i got this error on "user" in the line, you mentioned. This is the error -AuthRepository.user' ('Stream<User?> Function()') isn't a valid override of 'BaseAuthRepository.user' ('Stream<User> Function()').
  • Shreyansh Sharma
    Shreyansh Sharma almost 3 years
    On the "user", it is showing this error - 'AuthRepository.user' ('Stream<User?> Function()') isn't a valid override of 'BaseAuthRepository.user' ('Stream<User> Function()').
  • Victor Eronmosele
    Victor Eronmosele almost 3 years
    You need to make the same change in your BaseAuthRepository
  • Victor Eronmosele
    Victor Eronmosele almost 3 years
    Please read the guide to migrate to null safety: dart.dev/null-safety/migration-guide
  • Hemal Moradiya
    Hemal Moradiya almost 3 years
    It might be issue with your extended class BaseAuthRepository, bcs above line is the perfect with the null safety
  • Shreyansh Sharma
    Shreyansh Sharma almost 3 years
    Yes, it was. I really forgot about that. Thanks to you bro.
  • Shreyansh Sharma
    Shreyansh Sharma almost 3 years
    I really forgot about that. I made the changes and problem was solved.