Firestore CRUD Operations in Flutter

ahsan Avatar

·

·

Firestore is a NoSQL document database that is part of the Google Firebase platform. It is a popular choice for mobile app development because it is easy to use and provides real-time synchronization.

Firestore CRUD operations refer to the four basic operations that can be performed on data in a database:

  • Create: Add a new document to the database.
  • Read: Retrieve a document from the database.
  • Update: Modify an existing document in the database.
  • Delete: Remove a document from the database.

This blog post will discuss how to perform each of these operations in Firestore.

Creating Documents

To create a new document in Firestore, you can use the add() method. The add() method takes a map of data as its argument, and returns a Future object that resolves to the document ID of the newly created document.

// Create a new document in the `users` collection.
Future<String> createNewUser(String name, String email) async {
  final doc = await Firestore.instance.collection('users').add({
    'name': name,
    'email': email,
  });

  return doc.id;
}

Reading Documents

To read a document from Firestore, you can use the get() method. The get() method takes the document ID as its argument, and returns a Future object that resolves to a DocumentSnapshot object. The DocumentSnapshot object contains the data for the document, as well as its metadata.

// Get a document from the `users` collection by its document ID.
Future<DocumentSnapshot> getUser(String userId) async {
  final doc = await Firestore.instance.collection('users').doc(userId).get();

  return doc;
}

Updating Documents

To update a document in Firestore, you can use the update() method. The update() method takes a map of data as its argument, and updates the document with the new data.

// Update a document in the `users` collection by its document ID.
Future<void> updateUser(String userId, String name, String email) async {
  await Firestore.instance.collection('users').doc(userId).update({
    'name': name,
    'email': email,
  });
}

Deleting Documents

To delete a document from Firestore, you can use the delete() method. The delete() method takes the document ID as its argument, and deletes the document from the database.

// Delete a document from the `users` collection by its document ID.
Future<void> deleteUser(String userId) async {
  await Firestore.instance.collection('users').doc(userId).delete();
}

Batched Writes

Firestore also supports batched writes. Batched writes allow you to perform multiple CRUD operations on the database in a single transaction. This can be useful for improving performance and ensuring data consistency.

To perform a batched write, you can use the writeBatch() method. The writeBatch() method returns a WriteBatch object. You can then add CRUD operations to the WriteBatch object using the set(), update(), and delete() methods.

Once you have added all of the CRUD operations to the WriteBatch object, you can call the commit() method to execute the batch write.

// Perform a batched write to create two new documents in the `users` collection.
Future<void> createTwoUsers() async {
  final batch = Firestore.instance.batch();

  final user1 = User('Alice', '[email protected]');
  batch.set(Firestore.instance.collection('users').doc(user1.id), user1.toJson());

  final user2 = User('Bob', '[email protected]');
  batch.set(Firestore.instance.collection('users').doc(user2.id), user2.toJson());

  await batch.commit();
}

Conclusion

Firestore CRUD operations are relatively simple to perform. By following the examples in this blog post, you should be able to create, read, update, and delete documents in Firestore with ease.

Leave a Reply

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