CLASSFUNC BLOG
We Share Our Knowledge
Flutter Firebase Auth Google SignIn - Just for memo
Lê Thành
1 Th10 2025 10:47

flutter firebase google sigin.webp

Prepares:

  • Firebase project
  • [Firebase Authentiation tab] Google SignIn enabled

Steps:

  1. In Firebase console, create android app, following steps.
  2. Install Google Sigin package
  3. [Firebase Console] Add fingerprint:
# excute bellow cmd to get your keys's fingerprint (SHA1 & SHA256) # in project root cd android && ./gradlew signingReport

Example result:

Screenshot 2025-10-01 at 17.04.23.png
Then Copy SHA1 & SHA256 above to your firebase console android app.

3.1. [Important] Add fingerprint for your Production app:

  1. Code: e.g.: login.dart
Future<void> _signInWithGoogle() async { FocusScope.of(context).unfocus(); if (mounted) { setState(() { _isLoading = true; }); } try { if (Platform.isAndroid) { await _googleSignIn.initialize( /* optional serverClientId: normally in your downloaded android/app/google-services.json file, use WEB client id which "client_type": 3 */ serverClientId: "YOUR_oauth_client.client_id", ); } final GoogleSignInAccount googleUser = await _googleSignIn.authenticate(); final GoogleSignInAuthentication googleAuth = googleUser.authentication; final AuthCredential credential = GoogleAuthProvider.credential(idToken: googleAuth.idToken); UserCredential userCredential = await _auth.signInWithCredential(credential); if (kDebugMode) { print("Google Sign In Successful: ${userCredential.user?.uid}"); } if (mounted) { Navigator.of( context, ).pushReplacement(MaterialPageRoute(builder: (context) => const MainScreen())); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(e.toString()))); } if (kDebugMode) { print("Failed to sign in with Google: $e"); } } finally { if (mounted) setState(() => _isLoading = false); } }

Or modern code:

Future<void> _signInWithGoogle() async { FocusScope.of(context).unfocus(); if (mounted) { setState(() { _isLoading = true; }); } try { final GoogleSignIn signIn = GoogleSignIn.instance; unawaited( signIn .initialize( /* optional serverClientId: normally in your downloaded android/app/google-services.json file, use WEB client id which "client_type": 3 */ serverClientId: "YOUR_oauth_client.client_id", ) .then((_) { signIn.authenticationEvents.listen( (event) async { final GoogleSignInAccount googleUser; switch (event) { case GoogleSignInAuthenticationEventSignIn(): googleUser = event.user; case GoogleSignInAuthenticationEventSignOut(): // TODO: Handle this case. throw UnimplementedError(); } final GoogleSignInAuthentication googleAuth = googleUser.authentication; final AuthCredential credential = GoogleAuthProvider.credential( idToken: googleAuth.idToken, ); UserCredential userCredential = await _auth.signInWithCredential(credential); if (kDebugMode) { print("Google Sign In Successful: ${userCredential.user?.uid}"); } if (mounted) { Navigator.of( context, ).pushReplacement(MaterialPageRoute(builder: (context) => const MainScreen())); } }, onError: (e) => { if (mounted && !e.toString().contains("cancelled by the user")) { ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text(e.toString()))), }, }, onDone: () { setState(() { _isLoading = false; }); }, ); /// This example always uses the stream-based approach to determining /// which UI state to show, rather than using the future returned here, /// if any, to conditionally skip directly to the signed-in state. signIn.attemptLightweightAuthentication(); }), ); } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(e.toString()))); } if (kDebugMode) { print("Failed to sign in with Google: $e"); } } finally { if (mounted) setState(() => _isLoading = false); } }
  1. Your google sigin should worked as bellow:

Screen_Recording_20251001_172033.mp4

Also see some issues if have any questions: