有没有方法可以知道交易是否成功?如果我上传大型档案并花费太多时间,我需要实现加载“小部件动画”。然后在成功后更改屏幕,但我不知道如何。
谢谢!
交易示例:
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
});
});
在这种情况下,你不能从你的runTransaction
电话中收到,因为它返回一个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
将立即更新,服务器将返回给您。
要查看无法评估事务结果的原因,请在客户端检查answers on my question here。