Integrating Firebase with Flutter for Authentication

 Firebase and Flutter are a powerful combination for building modern, scalable mobile apps. Flutter is Google’s open-source UI toolkit, while Firebase provides backend services like authentication, databases, and analytics. One of the most commonly used features is Firebase Authentication, which supports email/password login, Google sign-in, phone number authentication, and more.

In this blog, we’ll walk through how to integrate Firebase Authentication into a Flutter app.

Set Up Firebase Project

Before writing code, create a Firebase project:

Go to Firebase Console

Click “Add Project” and follow the setup wizard

Add your Android and/or iOS app to the project

Download the configuration file:

For Android: google-services.json

For iOS: GoogleService-Info.plist

Add Firebase to Your Flutter App

Update your Flutter project to include Firebase:

a. Add dependencies in pubspec.yaml:

dependencies:

  firebase_core: ^latest_version

  firebase_auth: ^latest_version

Run flutter pub get to install packages.

b. Initialize Firebase

In your main.dart file:

import 'package:flutter/material.dart';

import 'package:firebase_core/firebase_core.dart';

void main() async {

  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp();

  runApp(MyApp());

}

Implement Email/Password Authentication

Here’s an example of signing up and signing in users:

import 'package:firebase_auth/firebase_auth.dart';

Future<void> registerUser(String email, String password) async {

  try {

    UserCredential userCredential = await FirebaseAuth.instance

        .createUserWithEmailAndPassword(email: email, password: password);

    print("User registered: ${userCredential.user?.email}");

  } catch (e) {

    print("Error: $e");

  }

}

Future<void> loginUser(String email, String password) async {

  try {

    UserCredential userCredential = await FirebaseAuth.instance

        .signInWithEmailAndPassword(email: email, password: password);

    print("User logged in: ${userCredential.user?.email}");

  } catch (e) {

    print("Login failed: $e");

  }

}

Sign-Out and User State

To sign out:

await FirebaseAuth.instance.signOut();

To listen for auth state changes:

FirebaseAuth.instance.authStateChanges().listen((User? user) {

  if (user == null) {

    print('User is signed out');

  } else {

    print('User is signed in: ${user.email}');

  }

});

Conclusion

Integrating Firebase Authentication in Flutter is quick and efficient. With just a few lines of code, you can enable secure user login and manage sessions. Firebase not only simplifies authentication but also scales effortlessly, making it ideal for startups and large-scale apps alike.

Learn Flutter Training in Hyderabad

Read More:

Working with Lists and Grids in Flutter

How to Add Navigation and Routing in Flutter Apps

Flutter Animations: Creating Smooth and Engaging UIs

Using Flutter Plugins and Packages to Extend Your App

Visit our IHub Talent Training Institute

Get Direction

Comments