How to Implement Google Sign In using Firebase in Flutter

Jan 20, 2024 | Programming

If you’re looking to integrate Google Sign In functionality into your Flutter application using Firebase, you’re in the right place! This guide will walk you through the steps necessary to make your app capable of authenticating users seamlessly via their Google accounts.

Understanding the Need for Google Sign In

Just as a key opens a door, Google Sign In unlocks access to your application with minimal friction for users. No more pesky username and password combinations—users can simply sign in with their Google account and get straight to using your app.

Requirements

  • Flutter SDK (Flutter 1.7 – for the sample app)
  • Firebase account
  • Basic knowledge of Flutter

Step-by-step Guide

Step 1: Generate the SHA-1

Open your terminal and use the following command to generate the SHA-1:

bash
keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore

Step 2: Complete the Firebase Setup

You need to configure Firebase for both Android and iOS platforms. While doing so, Firebase will provide you with two critical files: google-services.json for Android and GoogleService-Info.plist for iOS. Ensure you place these files in the respective directories.

Note: Generator SHA-1 from your system.

Step 3: Complete iOS Integration

For iOS, make sure to complete the changes noted in the Info.plist file as highlighted in the TODO comments.

Step 4: Run the App

Once everything is set up, run your app using the command:

bash
flutter run

Plugins Required

To implement Firebase Google Sign In, you’ll need the following plugins in your pubspec.yaml file:

yaml
dependencies:
  firebase_core: ^0.5.0
  firebase_auth: ^0.18.0+1
  google_sign_in: ^4.5.3

Importing the Packages

Start by importing the necessary packages in your Dart file:

dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:google_sign_in/google_sign_in.dart';

Authentication Methods

Here are two fundamental methods you can use to manage authentication via Google:

Sign In Method

dart
Future signInWithGoogle() async {
  await Firebase.initializeApp();
  final GoogleSignInAccount googleSignInAccount = await GoogleSignIn().signIn();
  final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;
  final AuthCredential credential = GoogleAuthProvider.credential(
    accessToken: googleSignInAuthentication.accessToken,
    idToken: googleSignInAuthentication.idToken,
  );
  final UserCredential authResult = await FirebaseAuth.instance.signInWithCredential(credential);
  final User user = authResult.user;
  assert(user.email != null);
  assert(user.displayName != null);
  assert(user.photoURL != null);
  return user;
}

Sign Out Method

dart
Future signOutGoogle() async {
  await GoogleSignIn().signOut();
  print('User Signed Out');
}

Troubleshooting Tips

  • If you face issues during setup, ensure that you have the correct SHA-1 fingerprint generated from your system.
  • Confirm that the google-services.json and GoogleService-Info.plist files are in their respective directories.
  • Check for any missing permissions in your project’s Android and iOS files.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox