Flutter Firebase.signInWithGoogle method not found

前端 未结 3 1216
星月不相逢
星月不相逢 2020-12-11 07:42

I am trying to add Google Authentication in my Flutter Application. But my Android studio is not able to find the method signInWithGoogle under Fire

相关标签:
3条回答
  • 2020-12-11 07:49

    Adding to the accepted answer, since firebase_auth version 0.12.0:

    Sign-in methods now return AuthResult instead of FirebaseUser. Retrieve the FirebaseUser using the user property of AuthResult.

    So the actual correct solution is:

    ...
        final AuthResult authResult = await _auth.signInWithCredential(credential);
        final FirebaseUser user = authResult.user;
    ...
    
    0 讨论(0)
  • 2020-12-11 08:08

    check out the example provided in the firebase_auth github repo https://github.com/flutter/plugins/blob/master/packages/firebase_auth/example/lib/main.dart#L70

    Future<String> _testSignInWithGoogle() async {
        final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
        final GoogleSignInAuthentication googleAuth =
            await googleUser.authentication;
        final AuthCredential credential = GoogleAuthProvider.getCredential(
          accessToken: googleAuth.accessToken,
          idToken: googleAuth.idToken,
        );
    
        final AuthResult authResult = await _auth.signInWithCredential(credential);
        final FirebaseUser user = authResult.user;
        assert(user.email != null);
        assert(user.displayName != null);
        assert(!user.isAnonymous);
        assert(await user.getIdToken() != null);
    
        final FirebaseUser currentUser = await _auth.currentUser();
        assert(user.uid == currentUser.uid);
    
        return 'signInWithGoogle succeeded: $user';
    }
    
    0 讨论(0)
  • 2020-12-11 08:13

    Instead of using fireebaseauth.signInWithGoogle (which is depreciated)
    use:

    AuthCredential credential = GoogleAuthProvider.getCredential(
        idToken: googleSignInAuthentication.idToken,
        accessToken: googleSignInAuthentication.accessToken
    );
    

    Your code will look like this:

    Future handleSign() async {
    preferences = await SharedPreferences.getInstance();
    setState(() {
      loading = true;
    });
    
    GoogleSignInAccount googleUser = await googleSignIn.signIn();
    GoogleSignInAuthentication googleSignInAuthentication = await googleUser
        .authentication;
    AuthCredential credential = GoogleAuthProvider.getCredential(
        idToken: googleSignInAuthentication.idToken,
        accessToken: googleSignInAuthentication.accessToken);
    }
    
    0 讨论(0)
提交回复
热议问题