One of the most powerful features of Firebase is the Realtime Database, a NoSQL database hosted in the cloud that allows you to store and synchronize data between your users in real time. This functionality is essential for creating interactive and dynamic applications, which require constant, real-time updates of user data. In this chapter, we are going to explore how to manage users in Realtime Database using Flutter and Dart.

Configuring the Firebase Realtime Database

Before we start managing users in the Realtime Database, we need to configure Firebase in our Flutter project. First, we need to add the Firebase Core and Firebase Database dependency in our pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  firebase_core: "^1.0.4"
  firebase_database: "^6.1.2"

Next, we need to initialize Firebase in our app. This can be done in the main method of our application, before running the application:

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

Managing Users in the Realtime Database

With Firebase configured, we can now start managing users in the Realtime Database. There are several operations we can perform, including creating, reading, updating, and deleting users.

Creating Users

To create a user in the Realtime Database, we first need to create a reference to the database. Then we can use the set method to add a user to the database:

final databaseReference = FirebaseDatabase.instance.reference();
await databaseReference.child('users').child(userId).set({
  'name': 'John Doe',
  'email': 'john.doe@example.com',
});

Reading Users

We can read a user's data from the Realtime Database using the once method. This method returns a Future that completes the data when it is loaded:

final databaseReference = FirebaseDatabase.instance.reference();
DataSnapshot dataSnapshot = await databaseReference.child('users').child(userId).once();
print('User name: ${dataSnapshot.value['name']}');
print('User email: ${dataSnapshot.value['email']}');

Updating Users

To update a user's data, we can use the update method. This method accepts a map with the new values ​​for the fields we want to update:

final databaseReference = FirebaseDatabase.instance.reference();
await databaseReference.child('users').child(userId).update({
  'name': 'Jane Doe',
});

Deleting Users

Finally, we can delete a user from the Realtime Database using the remove method:

final databaseReference = FirebaseDatabase.instance.reference();
await databaseReference.child('users').child(userId).remove();

As you can see, the Firebase Realtime Database provides an easy and efficient way to manage users in our Flutter apps. With these basic operations, we can create a variety of functionality, from user authentication to real-time data sharing between users.

Conclusion

In summary, the Firebase Realtime Database is a powerful tool for building interactive and dynamic applications. With it, we can manage users efficiently, performing creation, reading, updating and deleting operations in a simple and direct way. We hope this chapter has provided a clear overview of how to use the Firebase Realtime Database to manage users in your Flutter apps.

Now answer the exercise about the content:

What does Firebase Realtime Database let you do with users of a Flutter app?

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

You missed! Try again.

Article image Realtime Database with Firebase: Using Realtime Database in conjunction with other Firebase features

Next page of the Free Ebook:

234Realtime Database with Firebase: Using Realtime Database in conjunction with other Firebase features

4 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text