skip to Main Content
final batch = FirebaseFirestore.instance.batch();

var nycRef = FirebaseFirestore.instance.collection("FoodOrders").doc(); 
    // I want to specify a field here like where()
    // e.g var nycRef = FirebaseFirestore.instance.collection("FoodOrders").where("MyId",isEqualTo: myId);
batch.update(nycRef, {"Paid": true});
    
batch.commit();

But I get an error saying

The argument type ‘Query<Map<String, dynamic>>’ can’t be assigned to
the parameter type ‘DocumentReference<Object?>’.

Is there a way of filtering my batch write

I want to batch.update specifically where field MyId is equal to myId,how do I write it?

2

Answers


  1. Firestore doesn’t support updating all of the documents returned by a query like you can in SQL. The only way to do what you want is to perform each of these in sequence:

    1. Query for the documents.
    2. Iterate over all the documents in the resulting snapshot.
    3. For each document reference in the set, update it with new values
    Login or Signup to reply.
  2. try this as also described the steps by @DougStevenson .

    final batch = FirebaseFirestore.instance.batch();
    
    var query = FirebaseFirestore.instance.collection("FoodOrders").where("MyId", isEqualTo: myId);
    
    query.get().then((querySnapshot) {// query it 
      querySnapshot.docs.forEach((doc) { //and then iterate init
        batch.update(doc.reference, {"Paid": true});
      });
      
      batch.commit();
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search