Ad

Which Set Pattern Is Better To Use

I have come across two patterns of adding data to Cloud Firestore.

First is by using transactions, i.e:

Firestore.instance.runTransaction((transaction) async{
  await transaction.set(ref.document(), {
    'user_name': _name.text,
    'age': _age.text
  });
});

Second is by using setData() method:

Firestore.instance.collection('users').document('$id').setData({
  'user_name': _name.text,
  'age': _age.text
});

Is there any performance/good_practice reasoning behind using one or the other? Thanks!

Ad

Answer

You use a transaction when you need an atomic read-then-write operation. If you don't need to read the data at the location before writing it, then you don't need a transaction at all.

Ad
source: stackoverflow.com
Ad