I am not able to sign in to my google account in flutter by using google_sign_in

Viewed 159

I used google_sign_in in my project, included .json file, enabled google sign in methods from firebase, added sha1 and sha256 keys, still I am not able to sign into my account, can anyone help me please, I'm new to this and have no idea why is this showing up, the error is -

Launching lib\main.dart on sdk gphone x86 arm in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Waiting for sdk gphone x86 arm to report its views...
Debug service listening on ws://127.0.0.1:65344/pDLKVFAejJU=/ws
Syncing files to device sdk gphone x86 arm...
I/flutter (13131): Error in signing in : PlatformException(sign_in_required, com.google.android.gms.common.api.ApiException: 4: 4: , null, null)

Here's my code -

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:google_sign_in/google_sign_in.dart';

final GoogleSignIn googleSignIn = GoogleSignIn();

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  bool isAuth = false;

  @override
  void initState(){
    super.initState();
    googleSignIn.onCurrentUserChanged.listen((account) {
      handleSignIn(account);
    },
    onError: (error) {
      print("Error in signing in : $error");
    });
    googleSignIn.signInSilently(suppressErrors: false).then((account){
      handleSignIn(account);
    }).catchError((error){
      print("Error in signing in : $error");
    });
  }

  handleSignIn(GoogleSignInAccount account) {
    if (account != null) {
      print('The signed in user : $account');
      setState(() {
        isAuth = true;
      });
    }
    else {
      setState(() {
        isAuth = false;
      });
    }
  }

  login() {
    googleSignIn.signIn();
  }

  Widget buildAuthScreen(){
    return Text('Authenticated');
  }

  Scaffold buildUnauthScreen() {
    return Scaffold(
      backgroundColor: Colors.black,
      body: SafeArea(
        child: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topRight,
              end: Alignment.bottomLeft,
              colors: [
                Theme.of(context).primaryColor,
                Colors.teal.withOpacity(1.0),
                Colors.orange,
              ]
            ),
          ),
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Text(
                'Technua',
                style: GoogleFonts.gochiHand(
                  fontSize: 70.0,
                  color: Colors.white,
                ),
              ),
              GestureDetector(
                onTap: login,
                child: Container(
                  height: 60.0,
                  width: 260.0,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/google_signin_button.png'),
                      fit: BoxFit.cover,
                    )
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return isAuth ? buildAuthScreen() : buildUnauthScreen();
  }
}
1 Answers

You can try these steps:

1 - Fill all fields on the OAuth consent screen under the Google API console.

2 - Restart your project with CTRL+F5 instead of F5.

Here is a related issue on Github, and a highly voted solution on a similar issue.

Related