Realtime Database with Firebase: Removing Data from the Realtime Database

Capítulo 229

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

The Firebase Realtime Database is a cloud-hosted database that allows you to store and sync data between your users in real time. This allows data to be automatically updated across all connected devices in a matter of milliseconds. In this chapter, we will discuss how to remove data from Realtime Database using Flutter and Dart.

To begin with, it's important to understand that data in Firebase is stored as JSON and that any data can be accessed directly via a URL. This makes Firebase highly flexible and capable of handling a wide range of different data types.

To remove data, you must first identify the exact path of the data you want to remove. This can be done using the `child()` function to navigate through the data structure. For example, if you have a data structure like this:

{
  "users": {
    "user1": {
      "name": "John",
      "age": 30
    },
    "user2": {
      "name": "Jane",
      "age": 25
    }
  }
}

You can access the user 'John' with the following line of code:

var ref = FirebaseDatabase.instance.reference().child('users').child('user1');

Once you have a reference to the data you want to remove, you can use the `remove()` function to delete it. Here's how you can do that:

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

ref.remove();

This will remove the user 'John' from the database. Note that this will also remove all child data. In this case, it means that both the 'name' and 'age' of 'John' will be removed.

If you only want to remove a specific piece of data, you can do so by navigating to it with the `child()` function. For example, if you only want to remove the 'age' from 'John', you can do it like this:

var ref = FirebaseDatabase.instance.reference().child('users').child('user1').child('age');
ref.remove();

This will only remove the 'age' of 'John', leaving the 'name' intact.

It is important to note that the `remove()` function is asynchronous. This means that it returns a `Future` which you can use to find out when the remove operation has completed. You can do this as follows:

ref.remove().then((_) {
  print('Remove completed');
});

This will print 'Remove completed' to the console when the remove operation is complete.

In summary, removing data from Firebase's Realtime Database is a simple operation that involves identifying the path of the data you want to remove and calling the `remove()` function. Remember that the `remove()` function is asynchronous and returns a `Future` which you can use to find out when the operation has completed.

Understanding how to manipulate data in Firebase is crucial as this forms the foundation for building interactive and dynamic applications. In the next chapter, we'll discuss how to update data in Firebase, so stay tuned!

Now answer the exercise about the content:

How can you remove specific data from Firebase Realtime Database using Flutter and Dart?

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

You missed! Try again.

To remove specific data from Firebase Realtime Database using Flutter and Dart, you should navigate to the data using the child() function. Once you have the right reference, use the remove() function to delete it. This method allows you to precisely locate and remove exactly what you need from your database structure.

Next chapter

Realtime Database with Firebase: Advanced Queries in Realtime Database

Arrow Right Icon
Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course
86%

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

5

(4)

267 pages

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