FirebaseExtended / firestoreodm-flutter

BSD 3-Clause "New" or "Revised" License
35 stars 11 forks source link

[📚] [cloud_firestore_odm] Document how to use transaction in the update method #28

Closed TarekkMA closed 3 months ago

TarekkMA commented 3 months ago

Moved from: https://github.com/firebase/flutterfire/issues/10088

If I want to use transactionUpdate() method, I need to provide a Transaction object, but there is no documentation on how to create this object. For example, this code doesn't compile:

usersRef 
  .doc('foo_id')
  .transactionUpdate(
    transaction, // How to create this object? 
    name: 'John',
  );

I can create a Transaction object, using

var transaction = FirebaseFirestore.instance.runTransaction((txn) async {
  return txn;  
});

but using this object doesn't work in the first code. Here's the question on StackOverflow.

rrousselGit commented 3 months ago

The question on SO is answered already.

Your problem is unrelated to the ODM. The problem is that you have runTransaction return the Transaction object. That's not allowed. The Transaction object shouldn't be used outside of runTransaction.

You should instead put your transaction logic inside runTransaction.