Get Started with Realtime Database

Prerequisites

  1. Installfirebase_core and add the initialization codeto your app if you haven't already.
  2. Add your app to your Firebase project in theFirebase console.

Create a Database

  1. Navigate to theRealtime Database section of theFirebase console.You'll be prompted to select an existing Firebase project.Follow the database creation workflow.

  2. Select a starting mode for your security rules:

    Test mode

    Good for getting started with the mobile and web client libraries, but allows anyone to read and overwrite your data. After testing,make sure to review theUnderstand Firebase Realtime Database Rules section.

    Note: If you create a database in Test mode and make no changes to the default world-readable and world-writeable security rules within a trial period, you will be alerted by email, then your database rules will deny all requests. Note the expiration date during the Firebase console setup flow.

    To get started, select testmode.

    Locked mode

    Denies all reads and writes from mobile and web clients. Your authenticated application servers can still access your database.

  3. Choose a region for the database. Depending on your choice of region,the database namespace will be of the form<databaseName>.firebaseio.com or<databaseName>.<region>.firebasedatabase.app. For more information, seeselect locations for your project.

  4. ClickDone.

When you enable Realtime Database, it also enables the API in theCloud API Manager.

Add Firebase Realtime Database to your app

  1. From the root of your Flutter project, run the following command to install the plugin:

    flutterpubaddfirebase_database
  2. Once complete, rebuild your Flutter application:

    flutterrun

Configure database rules

The Realtime Database provides a declarative rules language that allows you todefine how your data should be structured, how it should be indexed, and whenyour data can be read from and written to.

Note: By default, read and write access to your database is restricted so onlyauthenticated users can read or write data. To get started without setting upFirebase Authentication, you canconfigure your rules for public access.This does make your database open to anyone, even people not using your app, sobe sure to restrict your database again when you set up authentication.

Initialize the Firebase Realtime Database package

To start using the Realtime Database package within your project, import it atthe top of your project files:

import'package:firebase_database/firebase_database.dart';

To use the default Database instance, call theinstancegetter onFirebaseDatabase:

FirebaseDatabasedatabase=FirebaseDatabase.instance;

If you'd like to use it with a secondary Firebase App, use the staticinstanceFor method:

FirebaseAppsecondaryApp=Firebase.app('SecondaryApp');FirebaseDatabasedatabase=FirebaseDatabase.instanceFor(app:secondaryApp);

If you'd like to use a different RTDB instance on the same project, you can pass in adatabaseUrl usingthe staticinstanceFor method:

finalfirebaseApp=Firebase.app();finalrtdb=FirebaseDatabase.instanceFor(app:firebaseApp,databaseURL:'https://your-realtime-database-url.firebaseio.com/');

Next Steps

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-11-13 UTC.