How to check if data was successfuly written to firebase in flutter

Viewed 1645

I need to check if data was written successfuly to Firestore so if it was successful I need to show a popup showing data was written or if data wasn't written i need to show a popup showing task failed so should I use a future builder what is the way to do it

2 Answers

Every Firestore Operations are promises, so it has .then() and .catch() method to handle whether the task is successful or failure.
For example, you can do something like this.

 firestoreInstance.collection("Users").document('123456').updateData({
 
  "username" : 'white_devil'

 }).then((result) {
   
   print("Success!");

 }).catch((error){
 
   print("Error!")

});

Not only on updateData(), you can also use this case in setData(),add(),get(), getDocuments() and all operations which returns a promise.

Related