google-cloud-firestore - 我如何知道我的 Firestore 交易是否失败?

标签 google-cloud-firestore flutter

我正在用这段代码更新我的文档。

Future<void> save() async {
  print('league save');
  final DocumentReference ref = 
    Firestore.instance.collection('leagues').document(_documentName);
Firestore.instance.runTransaction((Transaction tx) async {
  DocumentSnapshot postSnapshot = await tx.get(ref);
  if (postSnapshot.exists) {
    await tx.update(ref, _getDocument());
    print('league save complete');
  }
});
}

我相信这有时可能会失败,但我不确定。我遇到了错误。

我怀疑它有时会失败的原因是因为我的监听器(在应用程序的其他地方)在文档更改时并不总是被触发。

如何记录或捕获交易中的错误?

最佳答案

runTransaction 只是一个普通的异步操作,您可以跟进 then 和 catchError:

Firestore.instance.runTransaction((Transaction tx) async {

  // do whatever      

}).then((val) {

 // do something upon success

}).catchError((e) {

 // do something upon error 

});

如果你愿意,你可以跳过 then.then()

关于google-cloud-firestore - 我如何知道我的 Firestore 交易是否失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51510846/

相关文章:

flutter - 抖动的图像处理导致像素化

flutter - 小部件被释放后可以调用 addPostFrameCallback 吗?

android - 火库 : PERMISSION_DENIED: Missing or insufficient permissions

firebase - 向Cloud Firestore添加阵列(Flutter/Dart)

Javascript:Cloud Firestore 合并正在替换数据

android - 我正在尝试使用 "Pdf creation library"在 flutter 应用程序中生成 pdf。在用其他语言生成 pdf 时会出现异常

firebase - 如何在flutter中实现oauth登录?

android - Firestore : Found conflicting getters for name isText

firebase - 使用 firestore.rules 检查 auth.id 是否在资源数据映射中?

flutter - 在 Flutter 中使用 ListView 显示分段控件