Flutter — Google Sign In using firebase authentication[step-by-step]

DevLens
3 min readSep 29, 2023

--

This tutorial will guide you through the process of enabling Google Sign In authentication to flutter application using firebase authentication with step-by-step and show’s the user’s data.

Index

  1. Firebase setup
  2. Flutter code

Firebase setup

Before Firebase app setup, enable the Google sign-in method in the authentication section. Build> Authentication> Sign-in method.

Let’s get started…

If you not created firebase project yet, here are the steps->

  1. Android app: Let’s do the android app setup. click on the android app icons and follow these steps.

Note: 1.Add the downloaded ‘google-service.json’ file inside android>app folder.

2. Provide the SHA-1 fingerprint/ key while registering or after that to the app.

3. Add your project-level build.gradle file.

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.3'
}

2. iOS app: Similarly, we can do the iOS app setup. click on the iOS app icons and follow these steps.

Note: Add the downloaded ‘GoogleService-info.plist’ file inside iOS>Runner folder.

Flutter setup/ code

Add the dependencies to pubspec.yaml file under dependency section.run the following cmd for add latest version dependencies.

flutter pub add firebase_auth
flutter pub add google_sign_in

Sample UI code:

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

class GoogleSignInScreen extends StatefulWidget {
const GoogleSignInScreen({Key? key}) : super(key: key);

@override
State<GoogleSignInScreen> createState() => _GoogleSignInScreenState();
}

class _GoogleSignInScreenState extends State<GoogleSignInScreen> {
ValueNotifier userCredential = ValueNotifier('');

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Google SignIn Screen')),
body: ValueListenableBuilder(
valueListenable: userCredential,
builder: (context, value, child) {
return (userCredential.value == '' ||
userCredential.value == null)
? Center(
child: Card(
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
child: IconButton(
iconSize: 40,
icon: Image.asset(
'assets/images/google_icon.png',
),
onPressed: () async {
userCredential.value = await signInWithGoogle();
if (userCredential.value != null)
print(userCredential.value.user!.email);
},
),
),
)
: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: 1.5, color: Colors.black54)),
child: Image.network(
userCredential.value.user!.photoURL.toString()),
),
const SizedBox(
height: 20,
),
Text(userCredential.value.user!.displayName
.toString()),
const SizedBox(
height: 20,
),
Text(userCredential.value.user!.email.toString()),
const SizedBox(
height: 30,
),
ElevatedButton(
onPressed: () async {
bool result = await signOutFromGoogle();
if (result) userCredential.value = '';
},
child: const Text('Logout'))
],
),
);
}));
}

1.Google Sign-in Code

Future<dynamic> signInWithGoogle() async {
try {
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();

final GoogleSignInAuthentication? googleAuth =
await googleUser?.authentication;

final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);

return await FirebaseAuth.instance.signInWithCredential(credential);
} on Exception catch (e) {
// TODO
print('exception->$e');
}
}

2.Google Sign-out Code

Future<bool> signOutFromGoogle() async {
try {
await FirebaseAuth.instance.signOut();
return true;
} on Exception catch (_) {
return false;
}
}

Output

Thanks for reading!!

If you like this article, you can Buy Me a Coffee here.

--

--