Tired of setting up your own backend just to store and sync user data in real-time? Or struggling with database integration in Flutter that feels overly complex?

In this tutorial, you’ll learn how to effortlessly connect Firebase Realtime Database with your Flutter app — so you can start building dynamic, data-driven applications without spinning up a server or writing complex backend code.

What will we learn?

Following are the points we will cover in the blog.

  • Write and read live data from the Realtime Database.

  • Sync data in real-time across devices.

Introduction

Firebase Realtime database?

In modern mobile app development, real-time data management is crucial for delivering engaging and dynamic user experiences. Firebase Realtime Database offers a convenient solution for syncing data across devices in real-time. In this blog, we will explore how to integrate Firebase Realtime Database into a Flutter application, specifically focusing on curd (create, update, read, delete) operations. We will provide detailed code examples to help you get started with using Firebase Realtime Database in your Flutter projects.

Prerequisites

Before we jump to the implementation, we make sure we have following things setup on our machine

  1. Flutter SDK installed on your machine.

  2. A Firebase project created in the Firebase console (https://console.firebase.google.com).

  3. FlutterFire plugin dependencies added to your Flutter project (https://firebase.flutter.dev/docs/overview).

Step 1: Setting up Firebase Project:

  • Create a new Flutter project using the Flutter command-line tool or your preferred IDE.

  • Navigate to the Firebase console and create a new project.

  • Click on “Add Project name” and follow the setup instructions. This will provide you with a google-services.json file that you need to place in your Flutter project's android/app directory.

  • on the left side in build section, click on realtime database and initalize it.

Step 2: Adding Firebase Dependencies:

  • Open your Flutter project and navigate to the pubspec.yaml file.

  • Add the following dependencies under the dependencies section:

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^3.13.0
  firebase_database: ^11.3.5
  • Run flutter pub get to fetch the newly added dependencies.

Step 3: Initializing Firebase Realtime Database:

  • Open the main.dart file in your Flutter project.

  • Import the necessary Firebase and Flutter packages:

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
  • Initialize Firebase in the main() function:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

Perform CURD(Create, Update, Read, Read) Operation

Lets dive into the implementation of the CURD operations. we will learn step by step.

Initialize the instance

First we need to initialize the DatabaseReference instance. Then define the path name.

DatabaseReference userRef = FirebaseDatabase.instance.ref("User");

here “User” is the root-level node name. we will write data in this node.

Create Operation

To create a new record in the database, we can use push() method and then set() is used to write the data.

void create({required String id, required Map data}) {
    userRef.push().set(data).then((_) {
      print('Data written successfully!');
    }).catchError((error) {
      print('Failed to write data: $error');
    });
  }

By using the push() , firebase will create a random Id for the record. Something like below in the firebase:

items
  ├── -NkadJHSkdja3
  │     ├── name: "Item 1"
  ├── -Nkas8JDSKJf3
        ├── name: "Item 2"

Read Operation

To read the records from the firebase, we can fetch the value using the onValue property of the reference.

Stream read() {
   return userRef.onValue;
  }

This stream returns a DatabaseEvent, and we can access the actual data using event.snapshot.value.

In many cases, the returned data is a JSON-like structure, which we can cast to a Map<dynamic, dynamic> for easier processing.

You can listen to the stream using the listen((){ }) method.

void read() {
     userRef.onValue.map((event) {
      Map? values = event.snapshot.value as  Map?;
      values?.forEach((key, values) {
        print('Key: $key');
        print('Name: ${values['name']}');
        print('Email: ${values['email']}');
        print('Age: ${values['age']}');
      });
    });
  }

Delete Operation

We can remove the record using the remove() method. To remove the record from the database, we need to provide the id of the record.

void delete() {
// replace the id with your record Id.
    userRef.child("id").remove().then((_) {
      print('Data written successfully!');
    }).catchError((error) {
      print('Failed to write data: $error');
    });
  }

Update operation

To update the record we use update() method. We need to provide the recordId then call the set() method to set the new values against that recordId.

void update() {
// replace the id with your record Id.
    userRef.child("id").set({"username":"new value"}).then((_) {
      print('Data written successfully!');
    }).catchError((error) {
      print('Failed to write data: $error');
    });
  }

Create data using child() too.

We can also create a new record using the child() method. The only difference is that we need to provide the ID ourselves. If a record with this record ID does not exist, it will create the record; otherwise, it will update the record's value

void create() {
// replace the id with your record Id.
    userRef.child("id").set({"username":"new value"}).then((_) {
      print('Data written successfully!');
    }).catchError((error) {
      print('Failed to write data: $error');
    });
  }

Conclusion

In this lesson, we learned how to implement full CRUD operations—Create, Read, Update, and Delete—using Firebase Realtime Database in Flutter. You have learned how to organize your database, listen to real-time changes in data, and handle records effectively using Firebase functions such as set(), update(), remove(), and onValue.

Now you can develop real-time, cloud-connected applications that remain synchronized across devices. Continue to experiment, and also attempt to implement these ideas in your own projects to reinforce your knowledge!

Keep reading