randomUser786
randomUser786

Reputation: 1655

How to update a Firestore document after querying in Flutter

I am trying to figure out how to update my document after I query the one I want to update. Here is the code I came up with:

Firestore.instance.collection("Social_Posts").where(data["postTitle"], isEqualTo: "postTitle").where(data["postContent"], isEqualTo: "postContent")
                  .snapshots()
                  .listen((event) {print("hh");});

Whenever I perform this query to find the correct document, I would like to update the document by adding a number to it.

Upvotes: 0

Views: 581

Answers (2)

Abdul Malik
Abdul Malik

Reputation: 1188

It is possible to update the document from inside, The easier way to do that would definitely be to use the documentID and to increment the value Firestore has a special property, FieldValue.increment(1). Using this you can easily increment a field.

Lets say the field in the document you want to increment by 1, is "numberField".

Updated code using async/await style:

onTap: () async {

              setState(() {
                _title = data["postTitle"];
                _content= data["postContent"];
              });
            
              print(_title);
              print(_content);

              QuerySnaphot snapshot = await Firestore.instance.collection("Social_Posts")
                  .where("postTitle" , isEqualTo: _title)
                  .where("postContent", isEqualTo: _content)
                  .getDocuments();

        if(snapshot.documents.isNotEmpty)
        {
            snapshot.documents.forEach((doc){
                doc.reference.updateData({"views" : FieldValue.increment(1),});
            });

        }
}
             

The numberField will be incremented without needing to make an extra call to know the present value.

EDIT : You were making a mistake in the .where() method

Upvotes: 2

Sayed Ali AL-Kamel
Sayed Ali AL-Kamel

Reputation: 31

You can do that by using the document id you quired from inside the listen function in your case by using the event.documents[0].documentID assuming that your query returns one document only and then you can call the updateData method from Firestone package

Your code might looks like this:

Firestore.instance.collection("Social_Posts")
.where(data["postTitle"], isEqualTo: "postTitle")
    .where(data["postContent"], isEqualTo: "postContent")
    .snapshots()
    .listen((event) {

  Firestore.instance
      .collection("Social_Posts")
      .document(
      event.documents[0].documentID)
      .updateData(
      updateEvent) //Add here the new object you want to 
      .whenComplete(() {
    // You can add your desire action after the row is updated 
  }

 });

You can check package page for more information https://pub.dev/packages/cloud_firestore

And if you want to check a sample on how to perform CRUD functionalities you can check this repository: https://github.com/sayed3li97/flutter_firestore_crud

Upvotes: 1

Related Questions