2

I'm getting the below error:

The return type 'AuthUser?' isn't a 'User?', as required by the closure's context.

From the below code:

class AuthService {
  final FirebaseAuth authInstance = FirebaseAuth.instance;
  final FirebaseFirestore firestoreInstance = FirebaseFirestore.instance;

  Stream<User?> get currentUser => authInstance.authStateChanges().map(
        (User? firebaseUser) => (firebaseUser != null) ? AuthUser.fromFirebaseUser(user: firebaseUser) : null,
      );
}

Below is the code for the model class:

import 'package:firebase_auth/firebase_auth.dart';

class AuthUser {
  String? uid;
  String? email;
  String? userName;

  AuthUser({
    required this.uid,
    required this.email,
    required this.userName,
  });

  AuthUser.fromFirebaseUser({User? user}) {
    uid = user!.uid;
    email = user.email;
    userName = user.displayName;
  }
}

I would also like to know how to consume this stream using the StreamProvider<AuthUser>.value.

Please do let me know whether Consumer widget or the Provider.of(context) widget, which one would be appropriate to access the values.

Thanks in advance for your help.

xpetta
  • 607
  • 12
  • 28

2 Answers2

3

Your stream is mapping the User model from Firebase to your AuthUser model. So you need to change the return type of the currentUser getter.

Change this:

Stream<User?> get currentUser => authInstance.authStateChanges().map(
        (User? firebaseUser) => (firebaseUser != null) ? AuthUser.fromFirebaseUser(user: firebaseUser) : null,
);

to this:

Stream<AuthUser?> get currentUser => authInstance.authStateChanges().map(
        (User? firebaseUser) => (firebaseUser != null) ? AuthUser.fromFirebaseUser(user: firebaseUser) : null,
);
Victor Eronmosele
  • 7,040
  • 2
  • 10
  • 33
  • Thank you so much @Victor Eronmosele! As always brilliant and perfect! – xpetta Aug 13 '21 at 18:22
  • But when I try to access the uid or displayname or email, I'm unable to access them. `final authService = Provider.of(context);` `final currentLoggedInUser = authService.user;` – xpetta Aug 13 '21 at 18:44
  • Now if I try to make use of `currentLoggedInUser.uid` if doesn't gives me either the uid or displayName or email. Could you please help with this @Victor Eronmosele. Once again thank you so much for your help. – xpetta Aug 13 '21 at 18:47
-1

Auth have is own storage that store all this information all you need is to is to declear a global users then asses the infomation any place in the app...

example

User users

then example of authentication

final googleUser = await googleSignIn.signIn();
      final googleAuth = await googleUser.authentication;
      final AuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleAuth?.accessToken,
        idToken: googleAuth?.idToken,
      );
      users = (await auth.signInWithCredential(credential)).user;
      if (users == null) {
        return false;
      }
      return true;
    } catch (e) {
      print('this is error .......$e');
      return null;
    }
Gbenga B Ayannuga
  • 2,407
  • 3
  • 17
  • 38