
Prepares:
- Firebase project
- [Firebase Authentiation tab] Google SignIn enabled
Steps:
- In Firebase console, create android app, following steps.
- Install Google Sigin package
- [Firebase Console] Add fingerprint:
# excute bellow cmd to get your keys's fingerprint (SHA1 & SHA256) # in project root cd android && ./gradlew signingReport
Example result:
Then Copy SHA1 & SHA256 above to your firebase console android app.
3.1. [Important] Add fingerprint for your Production app:
- Go to play console >
App signing key certificate
then copy SHA1 & SHA256 to your firebase console android app: https://play.google.com/console/u/0/developers/DEVELOPER_ID/app/APP_ID/keymanagement
- 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); } }
- Your google sigin should worked as bellow: