Firebase Firestore Cloud Real-Time Database: Insert, Add, Set, and Update Methods Explained

Firebase Firestore Cloud Real-Time Database: Insert, Add, Set, and Update Methods Explained

Firebase Firestore is a cloud-hosted NoSQL document database that allows you to store, sync, and query data for your mobile and web applications. Firestore is part of the Firebase suite of tools and is fully integrated with Flutter, making it an excellent choice for managing data in real-time.

In this blog, we’ll explore Firestore’s methods for adding and updating data in real-time using Flutter and Dart. We’ll cover the insert, add, set, and update methods and provide code examples to demonstrate how they work.

1.Insert Method:

The insert method is used to add new data to a Firestore collection. Here is an example of how to use the insert method in Flutter:

final FirebaseFirestore firestore = FirebaseFirestore.instance;
CollectionReference usersRef = firestore.collection(‘users’);
usersRef.add({
‘name’: ‘John Doe’,
’email’: ‘johndoe@example.com’,
‘age’: 30
}).then((value) => print(“User Added”))
.catchError((error) => print(“Failed to add user: $error));

In the above example, we create a reference to the users collection in Firestore and use the add method to add a new document to the collection. The add method returns a Future that resolves to a DocumentReference for the newly added document.

2. Add Method:

The add method is similar to the insert method but allows you to add data to a specific document in a collection. Here is an example of how to use the add method in Flutter:

final FirebaseFirestore firestore = FirebaseFirestore.instance;
DocumentReference docRef = firestore.collection(‘users’).doc(‘john123’);
docRef.set({
‘name’: ‘John Doe’,
’email’: ‘johndoe@example.com’,
‘age’: 30
}).then((value) => print(“User Added/Updated”))
.catchError((error) => print(“Failed to add/update user: $error));

In the above example, we create a reference to the document with ID john123 in the users collection and use the collection method to reference the pets subcollection. We then use the add method to add a new document to the pets subcollection.

3.Set Method:

The set method is used to add or overwrite a document in a Firestore collection. Here is an example of how to use the set method in Flutter:

final FirebaseFirestore firestore = FirebaseFirestore.instance;
DocumentReference docRef = firestore.collection(‘users’).doc(‘john123’);
docRef.set({
‘name’: ‘John Doe’,
’email’: ‘johndoe@example.com’,
‘age’: 30
}).then((value) => print(“User Added/Updated”))
.catchError((error) => print(“Failed to add/update user: $error));

In the above example, we create a reference to the document with ID john123 in the users collection and use the set method to add or update the document with the specified data.

4. Update Method:

The update method is used to modify the existing data in a Firestore document. It allows you to update specific fields in a document without overwriting the entire document. Here is an example of how to use the update method in Flutter:

final FirebaseFirestore firestore = FirebaseFirestore.instance;
DocumentReference docRef = firestore.collection(‘users’).doc(‘john123’);
docRef.update({
‘age’: 26,
’email’: ‘john.doe@example.com’
}).then((value) => print(“User Updated”))
.catchError((error) => print(“Failed to update user: $error));

Leave a Reply

Your email address will not be published. Required fields are marked *