Search⌘ K
AI Features

Reading and Writing Data

Explore how to set up, read, and write data in Firebase Realtime Database within a Flutter app. Learn to model data, implement real-time updates with FirebaseAnimatedList, and manage JSON import and export. This lesson equips you with practical skills to build dynamic, data-driven applications using Firebase and Flutter.

Setting up

To get started with reading and writing data in Realtime Database, we need to first create the database in our project in the Firebase Console.

Firebase

Follow the steps below:

  1. Navigate to the “All products” menu and select “Realtime Database.”

  2. Continue by clicking “Create Database.”

  3. Select the preferred location.

  4. Select test mode to enable a quick setup.

The step-by-step guide to enabling Realtime Databases is illustrated in the following slides:

Flutter project

We then need to integrate Realtime Database in our Flutter app using the following steps:

  1. First, add the firebase_database package to the pubspec.yaml file in the flutter dependancies section.
  2. Next, add databaseURL to the main.dart file on the web Firebase initialization. The following code snippet show how this is achieved:
    
         apiKey: "AIzaSyAmOB_-8DIEIwJEOvrM2a8C6RE-KbvSDDc",
         projectId: "educative-9",
         storageBucket: "educative-9.appspot.com",
         //Here is the line
          databaseURL: "https://educative-9-default-rtdb.firebaseio.com", 
         messagingSenderId: "14205101883",
         appId:"1:14205101883:web:7c0db5d88bc2dc0310791e",
    
    

Note: The keys we’ve mentioned (apiKey, projectId, storageBucket, messagingSenderId, appId) are all part of the Firebase project configuration. These keys are required to initialize and authenticate our Firebase project when interacting with Firebase services on web platforms.

Writing data

To write data into our Realtime Database we’ll create a controller to handle this function and a data model to help with ...