如何在flutter中检查数据是否成功写入firebase

Nar*_*eit 4 dart firebase flutter google-cloud-firestore

我需要检查数据是否已成功写入 Firestore,因此如果成功,我需要显示一个弹出窗口,显示数据已写入,或者如果数据未写入,我需要显示一个弹出窗口,显示任务失败,所以我应该使用未来的构建器吗?就是这样做的方法

Shr*_*i L 6

每个 Firestore 操作都是 Promise,因此它有.then().catch()方法来处理任务是成功还是失败。
例如,你可以做这样的事情。

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

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

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

});
Run Code Online (Sandbox Code Playgroud)

不仅在 上updateData(),您还可以在setData()add()get()以及getDocuments()所有返回 Promise 的操作中使用这种情况。