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()!;
Author by
Shreyansh Sharma
Updated on December 30, 2022Comments
-
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 almost 3 yearsNo. it is again giving the same error. In your code snippet, return userChanges is giving the same error.
-
Shreyansh Sharma almost 3 yearsActually 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 almost 3 yearsOn 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 almost 3 yearsYou need to make the same change in your
BaseAuthRepository
-
Victor Eronmosele almost 3 yearsPlease read the guide to migrate to null safety: dart.dev/null-safety/migration-guide
-
Hemal Moradiya almost 3 yearsIt might be issue with your extended class
BaseAuthRepository
, bcs above line is the perfect with the null safety -
Shreyansh Sharma almost 3 yearsYes, it was. I really forgot about that. Thanks to you bro.
-
Shreyansh Sharma almost 3 yearsI really forgot about that. I made the changes and problem was solved.