How to Fix Google Sign-In Error in Flutter with Dart
Introduction Are you facing the frustrating 'PlatformException(sign_in_failed)' error in your Flutter project while trying to implement Google Sign-In? You are not alone. Many developers encounter this problem, especially when integrating Google Drive API in their applications. This article will provide you with a thorough understanding of this issue, its common causes, and step-by-step solutions to rectify it effectively. Understanding the Error The error you're encountering—PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null)—indicates that there is a configuration issue between your Flutter client and Google APIs, particularly when trying to authenticate users. This specific error (error code 10) typically arises due to one of the following reasons: OAuth 2.0 Client ID Misconfiguration: The OAuth 2.0 client ID generated might not be properly associated with your application's package name and SHA-1 fingerprint. Missing Google Services Configuration: The google-services.json file may not be properly placed or configured in your project. Inadequate Permissions: Your application might lack the necessary permissions to access Google Drive API. Common Causes of Google Sign-In Errors Let's explore some persistent issues that could lead to this error: 1. Incorrect Package Name or SHA-1 Fingerprint When you created the OAuth 2.0 client ID, you needed to specify the correct package name of your Flutter application along with its SHA-1 fingerprint. The mismatch can result in sign-in failures. To verify this: Go to the Google API Console. Select your project and navigate to APIs & Services > Credentials. Check your OAuth 2.0 Client IDs and validate the Package name and SHA-1 certificate fingerprint. 2. Missing google-services.json Ensure that you have correctly configured the google-services.json file: Download it from your Firebase Console. Place the file in the android/app/ directory of your Flutter project. 3. API Not Enabled Make sure that the Google Drive API has been enabled for your project: Go to the APIs & Services dashboard in your Google Cloud Console. Ensure that the Google Drive API is enabled. If not, enable it and retry. Step-by-Step Solution to Fix the Issue Here’s how to correctly implement Google Sign-In with proper configuration: Step 1: Setting Up Firebase and Google Sign-In Go to the Firebase Console and create a new project. Enable Google Sign-In under the Authentication section. Download the google-services.json file and place it in your android/app/ directory. Double-check the package name and SHA-1 in the OAuth consent screen. Step 2: Update Your Flutter Code Here is an example of how you should set up Google Sign-In in your Flutter project: import 'package:flutter/material.dart'; import 'package:google_sign_in/google_sign_in.dart'; final GoogleSignIn _googleSignIn = GoogleSignIn( scopes: [ 'https://www.googleapis.com/auth/drive', ], ); Future signInWithGoogle() async { try { await _googleSignIn.signIn(); // Handle successful sign in } catch (error) { print('Sign in failed: $error'); // Log error for debugging } } Step 3: Test the Configuration Run your Flutter application again and try signing in with Google. Ensure that you check the console for any further errors and address them accordingly. Frequently Asked Questions What if the error persists after following the steps? Review the google-services.json setup, and ensure the OAuth consent screen settings meet your app requirements. Double-check the SHA-1 fingerprint and package name. Can I use Google Sign-In without Firebase? While it's less common, you can authenticate directly through Google APIs without Firebase, but it requires a different set of configurations. Where can I find more resources about Google Sign-In? You can refer to the official Google Sign-In documentation for detailed guidelines and troubleshooting. Conclusion Integrating Google Sign-In in your Flutter app can be challenging, especially when it comes to correctly configuring the Google APIs and Firebase project. By following the guidelines and troubleshooting methods outlined in this article, you should be able to resolve the PlatformException(sign_in_failed) error effectively. Happy coding!

Introduction
Are you facing the frustrating 'PlatformException(sign_in_failed)' error in your Flutter project while trying to implement Google Sign-In? You are not alone. Many developers encounter this problem, especially when integrating Google Drive API in their applications. This article will provide you with a thorough understanding of this issue, its common causes, and step-by-step solutions to rectify it effectively.
Understanding the Error
The error you're encountering—PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null)
—indicates that there is a configuration issue between your Flutter client and Google APIs, particularly when trying to authenticate users.
This specific error (error code 10) typically arises due to one of the following reasons:
- OAuth 2.0 Client ID Misconfiguration: The OAuth 2.0 client ID generated might not be properly associated with your application's package name and SHA-1 fingerprint.
-
Missing Google Services Configuration: The
google-services.json
file may not be properly placed or configured in your project. - Inadequate Permissions: Your application might lack the necessary permissions to access Google Drive API.
Common Causes of Google Sign-In Errors
Let's explore some persistent issues that could lead to this error:
1. Incorrect Package Name or SHA-1 Fingerprint
When you created the OAuth 2.0 client ID, you needed to specify the correct package name of your Flutter application along with its SHA-1 fingerprint. The mismatch can result in sign-in failures. To verify this:
- Go to the Google API Console.
- Select your project and navigate to
APIs & Services > Credentials
. - Check your OAuth 2.0 Client IDs and validate the
Package name
andSHA-1 certificate fingerprint
.
2. Missing google-services.json
Ensure that you have correctly configured the google-services.json
file:
- Download it from your Firebase Console.
- Place the file in the
android/app/
directory of your Flutter project.
3. API Not Enabled
Make sure that the Google Drive API has been enabled for your project:
- Go to the APIs & Services dashboard in your Google Cloud Console.
- Ensure that the Google Drive API is enabled. If not, enable it and retry.
Step-by-Step Solution to Fix the Issue
Here’s how to correctly implement Google Sign-In with proper configuration:
Step 1: Setting Up Firebase and Google Sign-In
- Go to the Firebase Console and create a new project.
- Enable Google Sign-In under the
Authentication
section. - Download the
google-services.json
file and place it in yourandroid/app/
directory. - Double-check the package name and SHA-1 in the OAuth consent screen.
Step 2: Update Your Flutter Code
Here is an example of how you should set up Google Sign-In in your Flutter project:
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
final GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: [
'https://www.googleapis.com/auth/drive',
],
);
Future signInWithGoogle() async {
try {
await _googleSignIn.signIn();
// Handle successful sign in
} catch (error) {
print('Sign in failed: $error'); // Log error for debugging
}
}
Step 3: Test the Configuration
Run your Flutter application again and try signing in with Google. Ensure that you check the console for any further errors and address them accordingly.
Frequently Asked Questions
What if the error persists after following the steps?
- Review the
google-services.json
setup, and ensure the OAuth consent screen settings meet your app requirements. - Double-check the SHA-1 fingerprint and package name.
Can I use Google Sign-In without Firebase?
- While it's less common, you can authenticate directly through Google APIs without Firebase, but it requires a different set of configurations.
Where can I find more resources about Google Sign-In?
- You can refer to the official Google Sign-In documentation for detailed guidelines and troubleshooting.
Conclusion
Integrating Google Sign-In in your Flutter app can be challenging, especially when it comes to correctly configuring the Google APIs and Firebase project. By following the guidelines and troubleshooting methods outlined in this article, you should be able to resolve the PlatformException(sign_in_failed)
error effectively. Happy coding!