Integrating Google login in a Flutter app using Firebase involves several steps. Here’s a step-by-step guide to get you set up:
Prerequisites:
- Flutter SDK installed on your system.
- Firebase Account and a Firebase project created.
- Google Cloud Platform project with OAuth 2.0 credentials set up.
1. Create a Firebase Project
- Go to the Firebase Console.
- Click on “Add project” and follow the setup wizard to create a new project.
2. Add Firebase to Your Flutter App
For Android:
- In the Firebase Console, go to your project and click on the Android icon to add an Android app.
- Register your app by entering the package name (
com.example.appname
) and optionally other details. - Download the
google-services.json
file. - Place
google-services.json
in your Android project’sandroid/app
directory. - Modify your
android/build.gradle
file to include the Google services classpath in thedependencies
section:
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.3.15'
}
6. Modify your android/app/build.gradle
file to apply the Google services plugin at the bottom:
apply plugin: 'com.google.gms.google-services'
For iOS:
- In the Firebase Console, go to your project and click on the iOS icon to add an iOS app.
- Register your app by entering the iOS bundle ID and other details.
- Download the
GoogleService-Info.plist
file. - Place
GoogleService-Info.plist
in theios/Runner
directory. - Open
ios/Podfile
and ensure it’s set up to use frameworks:
platform :ios, '10.0'
use_frameworks!
- Run
pod install
in theios
directory.
3. Add Firebase Dependencies to Your Flutter App
- Open
pubspec.yaml
and add the required dependencies for Firebase and Google Sign-In:
dependencies:
flutter:
sdk: flutter
firebase_core: ^2.15.0
firebase_auth: ^4.15.0
google_sign_in: ^6.7.0
- Run
flutter pub get
to install the new dependencies.
4. Initialize Firebase in Your Flutter App
- Modify your
lib/main.dart
to ensure Firebase is initialized before the app starts:
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'firebase_options.dart'; // Automatically generated file
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
5. Set Up Google Sign-In
- Import the necessary packages in your
lib/home_screen.dart
or the relevant file where you want to implement the sign-in functionality:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
2. Create the sign-in logic:
class HomeScreen extends StatelessWidget {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
Future<void> _signInWithGoogle() async {
try {
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
if (googleUser == null) {
// User canceled the sign-in
return;
}
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await _auth.signInWithCredential(credential);
print("User signed in: ${_auth.currentUser?.displayName}");
} catch (e) {
print("Error during sign-in: $e");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Google Sign-In'),
),
body: Center(
child: ElevatedButton(
onPressed: _signInWithGoogle,
child: Text('Sign in with Google'),
),
),
);
}
}
6. Configure OAuth Consent Screen
- Go to the Google Cloud Console.
- Select the project linked to Firebase.
- Navigate to APIs & Services > OAuth consent screen.
- Configure the consent screen, specifying necessary details like app name and support email.
7. Add OAuth 2.0 Client IDs
- Go to APIs & Services > Credentials.
- Create OAuth 2.0 Client IDs for both Web application and Android.
- For Android, add your package name and SHA-1 fingerprint. For Web, ensure your redirect URIs are set up correctly.
Also Read:- Top 100 Flutter interview questions and answers in 2024
8. Test Your Implementation
Run your app on an emulator or device and test the Google Sign-In process.
By following these steps, you should be able to integrate Google Sign-In into your Flutter app using Firebase. If you encounter issues, check Firebase and Google Cloud documentation for troubleshooting.