DEV Community

Cover image for Google Authentication with Flutter
SUNDARAVEL
SUNDARAVEL

Posted on • Originally published at codesundar.com

Google Authentication with Flutter

Originally Published : How implement google login with Flutter

video Tutorial

Flutter Google Login

Add google_sign_in dependcies in pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  google_sign_in: ^4.0.2
Enter fullscreen mode Exit fullscreen mode

Update your main.dart as follows

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

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  bool _isLoggedIn = false;

  GoogleSignIn _googleSignIn = GoogleSignIn(scopes: ['email']);

  _login() async{
    try{
      await _googleSignIn.signIn();
      setState(() {
        _isLoggedIn = true;
      });
    } catch (err){
      print(err);
    }
  }

  _logout(){
    _googleSignIn.signOut();
    setState(() {
      _isLoggedIn = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        body: Center(
            child: _isLoggedIn
                ? Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Image.network(_googleSignIn.currentUser.photoUrl, height: 50.0, width: 50.0,),
                      Text(_googleSignIn.currentUser.displayName),
                      OutlineButton( child: Text("Logout"), onPressed: (){
                        _logout();
                      },)
                    ],
                  )
                : Center(
                    child: OutlineButton(
                      child: Text("Login with Google"),
                      onPressed: () {
                        _login();
                      },
                    ),
                  )),
      ),
    );
  }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
whitecoode profile image
Yahyah Ridwanullahi olanrewaju

I/flutter ( 5913): PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)