Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Firebase.signInWithGoogle method not found

I am trying to add Google Authentication in my Flutter Application. But my Android studio is not able to find the method signInWithGoogle under FirebaseAuth class. I mean to say when I write FirebaseAuth.signInWithGoogle Android studio complains saying The method signInWithGoogle isn't defined for the class Firebase.

I already have the have Firebase.signInWithEmailAndPassword and it is working fine.

I have below dependency in pubspec.yaml firebase_auth: ^0.8.0+1

Below are my imports in the class...

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:my_project/auth.dart';
import 'package:my_project/register.dart';
import 'package:my_project/signin/button_list.dart';
import 'package:my_project/signin/button_view.dart';
import 'package:my_project/utils.dart';
class _LoginPageState extends State<LoginPage> {
 final formKey = GlobalKey<FormState>();

// Google Sign In
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance; // No errors so far

below is the method in the same class that shows the error enter image description here

Also when I click the ctrl + space to see all the methods under FirebaseAuth it is not showing the signInWithGoogle method at all.

enter image description here

please help! Thanks in Advance...

like image 886
Sam Avatar asked Dec 03 '22 10:12

Sam


2 Answers

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';
}
like image 172
Harsha pulikollu Avatar answered Feb 23 '23 04:02

Harsha pulikollu


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;
...
like image 36
Gligor Atanasovski Avatar answered Feb 23 '23 05:02

Gligor Atanasovski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!