Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Realtime Database with Firebase: Writing Data to the Realtime Database

Capítulo 227

Estimated reading time: 4 minutes

Audio Icon

Listen in audio

0:00 / 0:00

When it comes to developing applications, one of the most important aspects is the ability to store and retrieve data in real time. This is especially true for applications that require real-time user interactions, such as games, chat applications, and location-sharing applications. In Flutter, one of the best ways to handle this is using the Firebase Realtime Database.

The Firebase Realtime Database is a database hosted in the cloud that allows you to store and synchronize data between your users in real time. The big benefit of this is that all of your users will always have the most up-to-date data, no matter where they are or what kind of internet connection they have.

To start using Firebase Realtime Database with Flutter, you need to first add the firebase_database package to your pubspec.yaml file. This will allow you to use all Firebase Realtime Database functionality in your Flutter app.

dependencies:
  flutter:
    sdk: flutter
  firebase_database: ^4.0.0

Once you've added the package, you can start using the Firebase Realtime Database in your app. The first thing you need to do is initialize Firebase in your app. You can do this in your application's main method.

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

With Firebase initialized, you can start writing data to the Realtime Database. To do this, you need to create a reference to the location in the database where you want to store your data. You can do this using the FirebaseDatabase object's reference method.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

final databaseReference = FirebaseDatabase.instance.reference();

With the reference created, you can start writing data to the database. To do this, you can use the set method of the database reference. The set method will replace all data at the reference location with the data you pass to it.

databaseReference.child('message').set('Hello World');

In this example, we are writing the string 'Hello World' to the 'message' location in the database. If the location 'message' does not exist, it will be created. If it already exists, all existing data will be replaced with the string 'Hello World'.

You can also write multiple values ​​at once using a Map. Each key in the Map will be a separate location in the database and the value associated with that key will be the value stored in that location.

databaseReference.child('user').set({
  'name': 'John Doe',
  'email': 'john.doe@example.com',
});

In this example, we are writing two values ​​to the 'user' location in the database. User name is 'John Doe' and email is 'john.doe@example.com'.

An important point to note is that the Firebase Realtime Database stores data in a tree structure. This means you can nest data by creating references to places deeper in the tree. For example, you can store the user's address in a separate location, nested within the user's location.

databaseReference.child('user/address').set({
  'street': '123 Main St',
  'city': 'Springfield',
  'state': 'IL',
  'zip': '12345',
});

In this example, we are writing four values ​​to the 'user/address' location in the database. Street is '123 Main St', city is 'Springfield', state is 'IL' and zip code is '12345'.

In short, the Firebase Realtime Database is a powerful tool for storing and synchronizing real-time data in your Flutter apps. With it, you can ensure that your users always have the most up-to-date data, no matter where they are or what type of Internet connection they have.

Now answer the exercise about the content:

What is the purpose of Firebase Realtime Database in Flutter app development?

You are right! Congratulations, now go to the next page

You missed! Try again.

The Firebase Realtime Database is designed to store and synchronize data between users in real time. This helps ensure that users always have access to the most up-to-date information, regardless of their location or internet connectivity. This aligns with Option 2, highlighting its role in real-time data handling in applications.

Next chapter

Realtime Database with Firebase: Updating Data in the Realtime Database

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.