Updating favors on Firebase

Before, when using mock data, we only needed to change our lists in memory. Now, we need to update our corresponding favor documents on Firebase so that this will trigger our previously defined callback, which will cause a rebuild and update our layouts.

We create a new method that will be used on every favor change, _updateFavorOnFirebase():

  void _updateFavorOnFirebase(Favor favor) async {
await Firestore.instance
.collection('favors') // 1
.document(favor.uuid) // 2
.setData(favor.toJson()); // 3
}

The beginning of the Firestore call is almost always the same: we get the Firestore instance, then we complete the following steps:

  1. We go to the favors collection.
  2. Then we get the reference of the favor document that we want to update.
  3. The last step is to send the data in JSON format to be updated in the corresponding document. The toJson() method is a simple converter to store on Firebase.
Check out the hands_on_firebase attached source code for the full code of conversions to and from Firebase.

The _updateFavorOnFirebase method is used on previously defined methods: complete, giveUp, acceptToDo, and refuseToDo. That's all we need to update on Firebase and reflect changes on the app layout.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.13.255