我怎么知道Firestore上的交易是否成功?

时间:2018-06-16 14:03:43

标签: firebase-realtime-database transactions dart google-cloud-firestore flutter

有没有方法可以知道交易是否成功?如果我上传大型档案并花费太多时间,我需要实现加载“小部件动画”。然后在成功后更改屏幕,但我不知道如何。

谢谢!

交易示例:

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

  CollectionReference reference = Firestore.instance.collection("pets_data");
  await reference

  .add({"name_pet":"${_nameController.text}",

  "age":"${_lifeController.text}",

  "photo_url":"$downloadableUrl",

  "editing":false,

  "score":0
});

});

1 个答案:

答案 0 :(得分:2)

在这种情况下,您无法通过C++ Reference来电,因为它会返回Future<Map<String, dynamic>>。由于已检查 ,它将始终返回 Map。因此,runTransaction函数本身无法获得任何东西。

您可以轻松地从您的参考文件中获取Stream更新,这看起来像这样:

DocumentReference documentReference;

firestore.runTransaction( // firestore in this case is your Firestore instance
  (Transaction transaction) async {
    // whatever you do here with your reference
    await transaction.update(
      documentReference,
      ...
);

documentReference.snapshots().listen((DocumentSnapshot event) {
  // here you could e.g. check if the transaction on your reference was succesful
});

正如您所见,我在相同 snapshots()上使用了Stream<DocumentSnapshot) DocumentReference,因为运行了事务。事务完成后Stream将立即更新 并且服务器会返回给您。

要查看无法评估事务结果的原因,请在客户端检查runTransaction

相关问题