Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The method 'signInWithGoogle' isn't defined for the class 'FirebaseAuth'

I am trying to learn how to get a flutter app to login to firebase auth. I've created a new flutter project using the android studio plugin and added the dependencies and code from the firebase_auth page.

I'm getting the error "The method 'signInWithGoogle' isn't defined for the class 'FirebaseAuth'" when trying to call the method in the FirebaseAuth.instance (_auth). Here's the code:

import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  final GoogleSignIn _googleSignIn = GoogleSignIn();
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<FirebaseUser> _handleSignIn() async {
    GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    GoogleSignInAuthentication googleAuth = await googleUser.authentication;
    FirebaseUser user = await _auth.signInWithGoogle(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    print("signed in " + user.displayName);
    return user;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[      // Add 3 lines from here...
          new IconButton(icon: const Icon(Icons.mic), onPressed: () {
            _handleSignIn()
                .then((FirebaseUser user) => print(user))
                .catchError((e) => print(e));
          })
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Pubspec.yaml:

name: flutter_auth
description: Trying out firebase_auth

version: 1.0.0+1

environment:
  sdk: ">=2.0.0-dev.68.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2

  google_sign_in: ^4.0.0
  firebase_auth: ^0.8.0+1

dev_dependencies:
  flutter_test:
    sdk: flutter


flutter:

  uses-material-design: true
like image 394
T. Cervenka Avatar asked Jan 30 '19 16:01

T. Cervenka


3 Answers

For Flutter 2.0

  final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

  await firebaseAuth.signInWithGoogle(idToken: googleAuth.accessToken, accessToken: googleAuth.idToken);
like image 143
Rafsan Uddin Beg Rizan Avatar answered Nov 05 '22 18:11

Rafsan Uddin Beg Rizan


I had the same problem and found the example in the firebase_auth

https://github.com/flutter/plugins/blob/master/packages/firebase_auth/example/lib/main.dart

Try replacing your handleSignIn method with

  Future<FirebaseUser> _handleSignIn() async {
    GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    GoogleSignInAuthentication googleAuth = await googleUser.authentication;
    final AuthCredential credential = GoogleAuthProvider.getCredential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    final FirebaseUser user = await _auth.signInWithCredential(credential);
    print("signed in " + user.displayName);
    return user;
  }

It may be that the signInWithGoogle method is valid but I couldn't find anything on it and the above code works for me.

like image 14
Paul Avatar answered Nov 05 '22 18:11

Paul


This implementation has been changed again, so now the _auth.signInWithCredential returns an AuthResult instance, and you can access to the user as a property of this object.

Future<FirebaseUser> _handleSignIn() async {
  GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  GoogleSignInAuthentication googleAuth = await googleUser.authentication;
  final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );
  final AuthResult authResult = await _auth.signInWithCredential(credential);
  FirebaseUser user = authResult.user;
  print("signed in " + user.displayName);
  return user;
}
like image 9
Freddy Guerrero Avatar answered Nov 05 '22 20:11

Freddy Guerrero