flutterfirebasedartfirebase-authenticationgoogle-oauth

Undefined class 'FirebaseUser'


I'm new to Flutter. I have an Issue with Firebase Auth/ Google Auth The FirebaseUser is not defined Code:

FirebaseAuth _auth = FirebaseAuth.instance;
GoogleSignIn googleSignIn = GoogleSignIn();

Future<FirebaseUser> currentUser() async { // The Issue is here in the Future<>
  final GoogleSignInAccount account = await googleSignIn.signIn();
  final GoogleSignInAuthentication authentication =
      await account.authentication;

  final GoogleAuthCredential credential = GoogleAuthProvider.getCredential(
      idToken: authentication.idToken, accessToken: authentication.accessToken);

  final AuthResult authResult = await _auth.signInWithCredential(credential);
  final FirebaseUser user = authResult.user; // and here as I can't define this FirebaseUser object to return

  return user;
}

Pubspec.yml

dependencies:
  flutter:
    sdk: flutter


  cupertino_icons: ^0.1.3
  firebase_auth: ^0.18.0
  location: ^3.0.2
  page_transition: ^1.1.6
  google_sign_in: ^4.5.1
  flutter_facebook_login: ^3.0.0
  firebase_database: ^4.0.0

I also face the same issue with AuthResult

final AuthResult authResult = await _auth.signInWithCredential(credential);

Solution

  • Starting from Version firebase_auth 0.18.0:

    In the newest version of firebase_auth, the class FirebaseUser was changed to User, and the class AuthResult was changed to UserCredential. Therefore change your code to the following:

        Future<User> currentUser() async {
          final GoogleSignInAccount account = await googleSignIn.signIn();
          final GoogleSignInAuthentication authentication =
              await account.authentication;
    
          final GoogleAuthCredential credential = GoogleAuthProvider.credential(
              idToken: authentication.idToken,
              accessToken: authentication.accessToken);
    
          final UserCredential authResult =
              await _auth.signInWithCredential(credential);
          final User user = authResult.user;
    
          return user;
        }
    

    FirebaseUser changed to User

    AuthResult changed to UserCredential

    GoogleAuthProvider.getCredential() changed to GoogleAuthProvider.credential()


    onAuthStateChanged which notifies about changes to the user's sign-in state was replaced with authStateChanges()

    currentUser() which is a method to retrieve the currently logged in user, was replaced with the property currentUser and it no longer returns a Future<FirebaseUser>.

    Example of the above two methods:

    FirebaseAuth.instance.authStateChanges().listen((event) {
       print(event.email);
    });
    

    And:

    final user = FirebaseAuth.instance.currentUser;
    print(user.uid);
    

    Deprecation of UserUpdateInfo class for firebaseUser.updateProfile method. Example:

    Future updateName(String name, FirebaseUser user) async {
      final userUpdateInfo = new UserUpdateInfo();
      userUpdateInfo.displayName = name;
      await user.updateProfile(userUpdateInfo);
      await user.reload(); 
    }
    

    now

    import 'package:firebase_auth/firebase_auth.dart' as firebaseAuth;
    Future updateName(String name, auth.User firebaseUser) async {
      firebaseUser.updateProfile(displayName: name);
      await firebaseUser.reload();
    }