In this article I’m gonna detail the steps to implement Login with Firebase and Google from Scratch in Flutter Android. You could start by following that steps on: https://firebase.flutter.dev/docs/overview/.
Create new Flutter – Hello World – Project.
Nothing complex here. You could execute it from any of the IDEs or the command line “flutter create <project_name>”.
Add dependencies and Setup
In your pubspec.yaml file, in the “dependencies:” area, add
firebase_core: "^1.0.3" firebase_auth: "^1.1.0" google_sign_in: ^5.0.0-nullsafety
- Firebase_core is the basis that connects to Firebase and opens the possibilities to use any of the Cloud Services within your app.
- Firebase_auth is the abstraction above all the ways a user could Authenticate against some service. It has methods for processing custom – new user registration and a common method for delegating Authentication to 3rd parties
For integrating Firebase Auth on Android, there are several more things to do:
-
- Setup and Download your google-services.json – generated on the Firebase Cloud Console into <project>/android/ – folder.
- You need to include the sha1 footprint in the Firebase Cloud Configuration: https://stackoverflow.com/questions/27609442/how-to-get-the-sha-1-fingerprint-certificate-in-android-studio-for-debug-mode.
- In the android/build.gradle – add the following dependency:
classpath 'com.google.gms:google-services:4.3.3'
- In the android/app/build.gradle – apply the following changes:
- apply plugin: ‘com.google.gms.google-services’ // along side the other apply plugin <…>
- defaultConfig {
multiDexEnabled true } -
include in the dependency section: implementation 'com.android.support:multidex:1.0.3'
Authenticate with Google
- If you want to use google_sign_in, a must for an Android Application you could do something like:
final GoogleSignInAuthentication googleAuth = await googleUser.authentication; // Create a new credential final OAuthCredential credential = GoogleAuthProvider.credential( accessToken: googleAuth.accessToken, idToken: googleAuth.idToken, ); // Once signed in, return the UserCredential return await FirebaseAuth.instance.signInWithCredential(credential);
- All the other options of acquiring external credentials are described here: https://firebase.flutter.dev/docs/auth/social
Side Tips and Fixes
In the beginning, I forgot to turn on – Sign in with Google in the Firebase Cloud Console. As a result I received an error similar to this one: https://github.com/flutter/flutter/issues/44538. I tried to reproduce it afterwards, but now, it returns the more meaningful message in the exception of “You forgot to turn on [Google Sign In] feature”. Anyway, if you get this, verify all configuration again. Listen and handle errors in:
GoogleSignIn().signIn().onError((error, stackTrace) {...}); If you plan to support null safety, do it at the beginning, so you do not have problems in the code to resolve and handle stuff right the first time. It is activated by bumping up the SDK in the pubspec.yaml:
environment: sdk: ">=2.12.0 <3.0.0"
You could watch a Video Demonstration of the integration on YouTube. I didn’t trimmed it so you could see all the problems that arise and how I found solution for them. Read more Flutter articles from my blog from the Flutter topic
