steps to google sign in flutter
Error
Google Sign-In Error: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 12500: , null, null)
Step-by-Step: Google Sign-In in Flutter (Without Firebase)
- Google Cloud Console Setup Go to the Google Cloud Console.
Create an OAuth 2.0 Client ID for Android.
Enter your app’s package name (e.g., com.cotocus.motoshare.partner).
Enter the SHA-1 certificate fingerprint from your Play Console’s "App signing key certificate" (as shown in your screenshot).
Save the configuration.
Note
:The SHA-1 certificate fingerprint in the Google Cloud Console matches the App signing key certificate SHA-1 from the Play Console.
- Flutter Project Setup Add the google_sign_in package to your pubspec.yaml.
No need to specify clientId for Android unless you have a special use case.
in pubspec.yml
google_sign_in: ^6.3.0
- Implement Google Sign-In Logic Create a service (GoogleAuthService) to handle the sign-in logic using the google_sign_in package.
In your UI, use a button (like your ElevatedButton.icon) to trigger the sign-in process.
import necessary package and create button
import 'package:google_sign_in/google_sign_in.dart';
import 'package:motoshare_partner/Screen/Auth/google_auth_service.dart';
final GoogleAuthService _authService = GoogleAuthService();
final GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: ['email', 'profile'],
);
ElevatedButton.icon(
icon: Image.asset(
'assets/google_logo.png', // Make sure this image exists in your assets
height: 24.0,
),
label: const Text('Sign in with Google'),
onPressed: _handleGoogleSignIn,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
backgroundColor: Colors.white, // Optional: match Google branding
foregroundColor: Colors.black, // Optional: match Google branding
minimumSize: const Size(double.infinity, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4.0),
side: const BorderSide(color: Color(0xFF4285F4)), // Optional: blue border
),
),
),
when i click button
Future<void> _handleGoogleSignIn() async {
setState(() => _isLoading = true);
try {
final GoogleSignInAccount? account = await _authService.signIn();
if (account != null) {
print('GoogleSignInAccountEmail: ${account.email}'); // Gmail ID (email)
print('GoogleSignInAccount Display Name: ${account.displayName}'); // Name (display name)
final loginViewModel = Provider.of<LoginViewModel>(context, listen: false);
// Call your login_email function with the Google account email
await loginViewModel.login_emails(account.email);
bool success = await loginViewModel.login_emails(account.email!); // ✅ Get status
print("mycode");
print(success);
if (success) { // Ensure widget is still mounted
print("i m inside");
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => DashboardScreen(email: account.email.toString())),
);
}
// _navigateToProfileScreen(account);
} else {
// Handle sign-in failure
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Sign in canceled or failed')),
);
}
} catch (error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error signing in: $error')),
);
} finally {
setState(() => _isLoading = false);
}
}
create one dart file
import 'package:motoshare_partner/Screen/Auth/google_auth_service.dart';
import 'package:google_sign_in/google_sign_in.dart';
class GoogleAuthService {
final GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: ['email', 'profile'],
// The clientId is optional for Android but required for web
);
// Sign in with Google
Future<GoogleSignInAccount?> signIn() async {
try {
// Try to sign in silently first (if user previously signed in)
GoogleSignInAccount? account = await _googleSignIn.signInSilently();
// If silent sign-in fails, show the sign-in UI
account ??= await _googleSignIn.signIn();
if (account != null) {
final GoogleSignInAuthentication googleAuth = await account.authentication;
// You can use googleAuth.accessToken and googleAuth.idToken as needed
print('Access Token: ${googleAuth.accessToken}');
print('ID Token: ${googleAuth.idToken}');
print('Email: ${account.email}'); // Gmail ID (email)
print('Display Name: ${account.displayName}'); // Name (display name)
}
return account;
} catch (error) {
print('Google Sign-In Error: $error');
return null;
}
}
// Sign out from Google
Future<void> signOut() async {
try {
await _googleSignIn.signOut();
print('User signed out successfully');
} catch (error) {
print('Sign Out Error: $error');
}
}
// Check if user is signed in
Future<bool> isSignedIn() async {
return await _googleSignIn.isSignedIn();
}
// Get current user
GoogleSignInAccount? getCurrentUser() {
return _googleSignIn.currentUser;
}
}
- Sign-In Flow in Your Code When the user taps the Google sign-in button, _handleGoogleSignIn is called.
This calls your GoogleAuthService.signIn() method:
First, it tries to sign in silently (if the user is already authenticated).
If not, it shows the Google sign-in UI.
On success, you get a GoogleSignInAccount object with user info (email, display name, etc.).
You can also access the user’s accessToken and idToken if needed.
Your app then uses the signed-in user’s email to check or create an account in your backend (as in your login_emails call).
If the login is successful, you navigate to the dashboard.
- Production Release Considerations The SHA-1 in Google Cloud Console must match the Play Console’s App signing key (which you have done).
When users download your app from the Play Store, Google will only allow sign-in if the SHA-1 and package name match what you set in the Cloud Console.
Google Sign-In Error: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 12500: , null, null)
note
Add All Relevant SHA-1 Keys
You must add both:
The SHA-1 from the Google Play Console (for production)
The SHA-1 from your local debug keystore (for local development and emulator)
Steps:
Get your debug SHA-1:
On Windows:
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
Top comments (0)