Implementing Email and Password Authentication in Flutter using Firebase | Flutter & Firebase

Based in India.
Search for a command to run...

Based in India.
No comments yet. Be the first to comment.
In this series I'll publish blogs on different functionalities/ features we can make using Flutter and Firebase.
I recently had to deploy a Ktor project to Railway for my hobby project. I'll share the how-to through this article. Introduction Ktor is a framework that allows us to build web applications in Kotlin, built and backed by Jetbrains. It's open-source,...

Understanding Coroutines and Flow Kotlin Coroutines and Flows are a powerful tool for managing asynchronous programming in your Android apps. This article is a beginner's guide to understanding Kotlin Flow. First, let's understand what coroutines are...

The problem When working with JVM (Java Virtual Machine), the Garbage Collector will take care of allocating and deallocating memory for us - Most of the time. In some cases, it fails to release the memory for some objects which are no longer in use,...

What are quick actions? Quick actions under the hood are App Shortcuts(in Android) and Home Screen Actions(in iOS) which allow users to perform certain app actions without having to open the app. Where have I seen this? You might have already seen an...

What are app shortcuts? App shortcuts (introduced in Android Nougat 7.1) allow users to perform certain app actions without having to open the app. Developers can allow users to pin certain app actions to home screen. Let's see how. Where have I seen...

This is the beginning of Flutter and Firebase series. In this series, we’ll implement different Firebase services, one service at a time. For this article, we’ll see how to implement Firebase Authentication with email and password.
Let’s get started!
Prerequisites:
Installed Flutter and Dart SDK,
Android Studio / Visual Studio Code,
Able to run the default Flutter app.
Follow the official Firebase documentation from here to setup Firebase in the project. Then, add the following in pubspec.yaml —
dev_dependencies:
flutter_test:
sdk: flutter
firebase_core:
firebase_auth:
and do flutter packages get
Don’t forget to enable Email/Password Sign-in.

Firebase Console-> Authentication.
To make our app backward compatible, we need to enable androidX. To do so, follow the instructions here.
TextFormFields and ButtonWe’ll reuse the following code for creating both Register and Login interface —
Scaffold(
body: Container(
padding: EdgeInsets.all(16),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
controller: emailTC,
decoration: InputDecoration(labelText: "Email")),
TextFormField(
controller: passwordTC,
obscureText: true,
decoration: InputDecoration(labelText: "Password"),
),
FlatButton(
child: Text("Submit"),
textColor: Colors.white,
color: Colors.blue,
onPressed: () {
doSomething(emailTC.text, passwordTC.text);
}),
],
)),
),
);
We’ll create a function named register to sign up users using email and password —
// Initializing Firebase Auth
final *FirebaseAuth* _auth = *FirebaseAuth*.instance;
// Defining Register function
register(emailAddr, pass) {
_auth.createUserWithEmailAndPassword(email: emailAddr, password: pass)
.then((FirebaseUser user) => print(user))
.catchError((e) => print(e));
}
To implement login with email and password, we’ll create a login function —
// Initializing Firebase Auth
final *FirebaseAuth* _auth = *FirebaseAuth*.instance;
// Defining Login function
login(emailAddr, pass) {
_auth.signInWithEmailAndPassword(email: emailAddr, password: pass)
.then((FirebaseUser user) => print(user))
.catchError((e) => print(e));
}
You might get PlatformException (PlatformException(error, Given String is empty or null, null), to handle that we used .catchError() method above. This can be referenced in this issue.
Well, that all!
If you followed every step properly, the code will work flawlessly unless the Flutter developers decide to change any method implemented here.
All the functionalities above are implemented in this repository.