Setting Up Firebase for Google Authentication in Flutter
To integrate Google Authentication into your Flutter app using Firebase, follow the steps below.
1. Create a Firebase Project
- Sign in to Firebase Console: Navigate to the Firebase Console and sign in with your Google account.
- Add a New Project: Click on "Add project" and follow the on-screen instructions to create a new project.
2. Register Your App with Firebase
For Android:
- Add Android App: In the Firebase Console, select your project, then click on the Android icon to add an Android app.
- Register App: Enter your app's package name (found in your Flutter project's
android/app/src/main/AndroidManifest.xml), and provide a nickname and SHA-1 key if needed. - Download
google-services.json: After registering, download thegoogle-services.jsonfile and place it in your Flutter project atandroid/app/. - Modify
build.gradleFiles:- Project-level (
android/build.gradle): Add the classpath to thedependenciessection. - App-level (
android/app/build.gradle): Apply the Google services plugin and add thegoogle-services.jsonfile.
- Project-level (
For iOS:
- Add iOS App: In the Firebase Console, click on the iOS icon to add an iOS app.
- Register App: Enter your iOS bundle ID (found in
ios/Runner.xcodeproj), and provide other required information. - Download
GoogleService-Info.plist: After registering, download theGoogleService-Info.plistfile and place it in your Flutter project atios/Runner/. - Modify
ios/Podfile: Ensure the platform is set to iOS 10 or later. - Install Pods: Run
pod installin theiosdirectory.
3. Enable Google Sign-In in Firebase
- Navigate to Authentication: In the Firebase Console, go to the "Authentication" section.
- Set Up Sign-In Method: Under the "Sign-in method" tab, enable "Google" as a sign-in provider and configure the necessary settings.
4. Initialize Firebase in Your Flutter App
In your main.dart file, ensure that Firebase is initialized before running the app.
5. Implement Google Sign-In
Utilize the firebase_auth and google_sign_in packages to handle authentication.
6. Test Authentication
Run your app on a device or emulator to test the Google Sign-In functionality. Ensure that the Google account used is added as a tester in your Firebase project if your app isn't publicly released.
For more detailed information, refer to the Firebase Authentication for Flutter guide.