Firestore transaction fails with "Transaction failed all retries"
1,652
This has recently been fixed - https://github.com/flutter/plugins/pull/1206.
If you are using the master channel, the fix should be available already. For the other channels (dev, beta, stable) YMMV.
Author by
Duncan Jones
Updated on December 05, 2022Comments
-
Duncan Jones over 1 year
I'm running a very simple Firestore transaction which checks for the presence of a document, before writing to it if absent.
(The use case is registering a username - if it's not already registered, the current user gets to grab it)
Here's a snippet of the relevant Flutter code:
DocumentReference usernameDocRef = Firestore.instance.collection(_USERNAMES).document(username); await Firestore.instance.runTransaction((transaction) async { var snapshot = await transaction.get(usernameDocRef); if (!snapshot.exists) { transaction.set(usernameDocRef, { _UsernamesKey.userid: _user.id, }); } });
This is failing with an exception "Transaction failed all retries".
Based on the Firestore documentation, failure can occur for two reasons:
- The transaction contains read operations after write operations. Read operations must always come before any write operations.
- The transaction read a document that was modified outside of the transaction. In this case, the transaction automatically runs again. The transaction is retried a finite number of times.
I don't think I trigger either of those. Any suggestions?