title: Update multiple documents in a single transaction with dart and Firestore
body: <p>I'm trying to create a <strong>save transaction</strong> with Flutter / Dart and Firestore which does the following:</p>
  
  <ol>
  <li>Add a new item with attribute "position: 1" to a collection of documents</li>
  <li>Increment the "position" attributes of all other documents in this collection with a position > 1 by 1 (++)</li>
  </ol>
  
  <p>A single document looks like this:</p>
  
  <pre><code>{
      "title": "Some title",
      "position": 1
  }
  </code></pre>
  
  <p>In the documentation (<a href="https://firebase.google.com/docs/firestore/manage-data/transactions" rel="noreferrer">https://firebase.google.com/docs/firestore/manage-data/transactions</a>) I found explanation on how to make a save transaction on a single document but not on how to create this "two-step-atomic-transaction" that I need to save insert and update in a collection.</p>
  
  <p>Maybe anyone has a hint for me?</p>
  
  <p><strong>Update:</strong>
  Thanks for your input Frank. Here is a code snippet which hopefully explains what I want to achieve:</p>
  
  <pre><code>@override
  Future addCatalogItem(CatalogItem catalogItem) {
  
   return firestore.runTransaction((Transaction transaction) async {
  
    // Update position (++) of existing catalogItems
    await firestore
        .collection(path)
        .where("catalogId", isEqualTo: catalogItem.catalogId)
        .snapshots()
        .forEach((snapshot) {
      snapshot.documents.forEach((document) {
        // document.reference.updateData({"position": document.data["position"]});
        transaction.update(document.reference, {"position": document.data["position"] + 1});
      });
    });
  
    // Add the new catalogItem at position 1
    await firestore
        .collection(path)
        .document(catalogItem.id)
        .setData(catalogItem.toJson());
  
  });
  </code></pre>
  
  <p>}</p>