Integrating Google Sign-In in Flutter: A Step-by-Step Guide

Google Sign-In is a secure authentication system provided by Google that allows users to authenticate themselves to access various Google services, including Firebase. In Flutter, Google Sign-In is a method of authenticating users by enabling them to sign in with their Google credentials. This allows users to access your app using their existing Google account, without having to create a new account or provide additional authentication information.

Flutter provides a google_sign_in package, which is an implementation of the Google Sign-In API for Flutter apps. This package provides a simple way to integrate Google Sign-In into your Flutter app and enables users to sign in with their Google account in just a few steps. Once a user is authenticated, you can use their credentials to provide access to other Google services, such as Firebase, Google Drive, and Google Calendar, among others.

google_sign_in_flutter

Integrating Google Sign-In into a Flutter app involves a few steps:

  1. Create a new Firebase project or use an existing one.
  2. Add a new Android app or iOS app to your Firebase project and follow the instructions to download and add the configuration files to your Flutter app.
  3. Add the necessary dependencies to your Flutter app's pubspec.yaml file.
  4. Add the required code to your Flutter app's Dart files.

Here are the detailed steps to integrate Google Sign-In into your Flutter app:

Step 1: Create a new Firebase project or use an existing one

If you don't have a Firebase project already set up, follow these instructions to create a new one:

  • Go to the Firebase console at https://console.firebase.google.com/.
  • Click the "Create Project" button, give your project a name, and click "Create Project".
  • Follow the prompts to add Firebase to your Android or iOS app. Make sure to download the configuration files and save them in a safe location. You'll need these files later to add the necessary information to your Flutter app.
google_sign_in_flutter

Step 2: Add a new Android app or iOS app to your Firebase project

  • In the Firebase console, select your project and click the "Add app" button.
  • Choose either Android or iOS and follow the prompts to add a new app to your project.
  • For Android, you'll need to provide your app's package name, and for iOS, you'll need to provide your app's bundle ID.
Firebase_to_mobile_app

Step 3: Add the necessary dependencies to your Flutter app's pubspec.yaml file

  • Add the following dependencies to your pubspec.yaml file:
dependencies:
  firebase_core: ^1.6.0
  firebase_auth: ^3.3.0
  google_sign_in: ^5.2.1

Step 4: Add the required code to your Flutter app's Dart files

  • Import the necessary packages in your Dart file:
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
  • Initialize Firebase in your app:
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
  • Create an instance of GoogleSignIn:
final GoogleSignIn googleSignIn = GoogleSignIn();
  • Create a button that the user can click to sign in with Google:
ElevatedButton(
  onPressed: () async {
    final GoogleSignInAccount? googleUser = await googleSignIn.signIn();

    if (googleUser != null) {
      final GoogleSignInAuthentication googleAuth =
          await googleUser.authentication;

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

      final UserCredential userCredential =
          await FirebaseAuth.instance.signInWithCredential(credential);

      final User? user = userCredential.user;
    }
  },
  child: Text('Sign in with Google'),
)

This code creates an ElevatedButton widget with a callback function that allows a user to sign in with their Google account in a Flutter app. 

Here's what each line does:
  • Creates an ElevatedButton widget with an onPressed callback function.
  • Defines an anonymous asynchronous function that will be called when the button is pressed.
  • Declares a variable googleUser and assigns the result of googleSignIn.signIn(), which is an asynchronous call to initiate the Google sign-in process. The await keyword is used to wait for the result of this function call.
  • Checks if the googleUser object is not null, meaning the user has successfully signed in.
  • Declares a variable googleAuth and assigns the result of googleUser.authentication, which is an asynchronous call to authenticate the user with Google. The await keyword is used to wait for the result of this function call.
  • Declares a variable credential and assigns a new OAuthCredential object created with the user's accessToken and idToken. These tokens are used to authenticate the user with Firebase.
  • Declares a variable userCredential and assigns the result of FirebaseAuth.instance.signInWithCredential(credential), which is an asynchronous call to sign in the user with Firebase using the credential object. The await keyword is used to wait for the result of this function call.
  • Declares a variable user and assigns the user property of the userCredential object, which contains information about the signed-in user.
  • Specifies the child of the ElevatedButton, which is a Text widget that displays the text "Sign in with Google".

Comments

Popular posts from this blog

Error Handling in Flutter - Gradle issue

How to Make a Dynamic and Trending ListView with Flutter Widgets?

Understanding API integration with Getx State management