firebase_coreand add the initialization code to your app if you haven't already.
- Add your app to your Firebase project in the Firebase console.
Create a Database
Navigate to the Realtime Database section of the Firebase console. You'll be prompted to select an existing Firebase project. Follow the database creation workflow.
Select a starting mode for your security rules:
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 the Understand Firebase Realtime Database Rules section.
To get started, select testmode.
Denies all reads and writes from mobile and web clients. Your authenticated application servers can still access your database.
Choose a region for the database. Depending on your choice of region, the database namespace will be of the form
<databaseName>.<region>.firebasedatabase.app. For more information, see select locations for your project.
When you enable Realtime Database, it also enables the API in the Cloud API Manager.
Add Firebase Realtime Database to your app
From the root of your Flutter project, run the following command to install the plugin:
flutter pub add firebase_database
Once complete, rebuild your Flutter application:
Configure database rules
The Realtime Database provides a declarative rules language that allows you to define how your data should be structured, how it should be indexed, and when your data can be read from and written to.
Initialize the Firebase Realtime Database package
To start using the Realtime Database package within your project, import it at the top of your project files:
To use the default Database instance, call the
FirebaseDatabase database = FirebaseDatabase.instance;
If you'd like to use it with a secondary Firebase App, use the static
FirebaseApp secondaryApp = Firebase.app('SecondaryApp'); FirebaseDatabase database = FirebaseDatabase.instanceFor(app: secondaryApp);
If you'd like to use a different RTDB instance on the same project, you can pass in a
final firebaseApp = Firebase.app(); final rtdb = FirebaseDatabase.instanceFor(app: firebaseApp, databaseURL: 'https://your-realtime-database-url.firebaseio.com/');
Learn how to structure data for Realtime Database.