How do I know if a transaction on Firestore succeded?
You cannot receive that from your runTransaction
call in this case because it returns a Future<Map<String, dynamic>>
. As I have checked, it will always return an empty Map
. Thus there is nothing to get from the runTransaction
function itself.
You can easily get a Stream
of updates from your reference though, which would look something like this:
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
});
As you can see I used the snapshots()
Stream<DocumentSnapshot)
on the same DocumentReference
as the transaction was run on. The Stream
will update as soon as the transaction has completed and the server reached back to you.
To see why transaction results cannot be evaluated client-side check the answers on my question here.
Eduardo Yamauchi
Updated on December 05, 2022Comments
-
Eduardo Yamauchi over 1 year
Is there any method to know if the transaction was successful? I need to implement a loading "widget animation" if I upload large archives and spend too much time on that. And then change the screen after success, but I don't know-how.
Thanks!
Transaction Example:
CollectionReference reference = Firestore.instance.collection("collection_example"); Firestore.instance.runTransaction((Transaction transaction) async { await transaction.set(reference, { "index_1":"ABC", "index_2": 2, "index_3": {"mapIndex_1": "ABC"} }); });