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: Managing Users in Realtime Database

Capítulo 233

Estimated reading time: 4 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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:

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();
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.

The Firebase Realtime Database provides operations for creating, reading, updating, and deleting users in a Flutter app, allowing real-time synchronization of user data.

Next chapter

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

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