How to solve: Flutter Firestore Update Where


Asked by Martin on January 03, 2022 (source).

I'm trying to run a query that retrieves a single row given a where clause and updates it. I understand that Firebase doesn't support an UpdateWhere operations so I'm trying to use a Transaction instead.

I'm having difficulty making it work, maybe I'm too used to sql dbs... Here's my broken code

try {
    final whereQuery = _db
        .where("active", isEqualTo: true)

    await _db.runTransaction((transaction) async {
        final entry = await transaction.get(whereQuery);  // This doesn't compile as .get doesn't take in a query
        await transaction.update(entry, {
            "someValue": "newValue",
} catch (e) {


Question answered by Rogelio M (source).

From the test I’ve made, I would suggest the following to achieve what you mention:

Based on the following answer:

As you can see from the API documentation, where() returns a Query object. It's not a DocumentReference.

Even if you think that a query will only return one document, you still have to write code to deal with the fact that it could return zero or more documents in a QuerySnapshot object. I suggest reviewing the documentation on queries to see examples.

After doing the query consult, you have to get the DocumentReference for that given result.

Then, you can use that reference to update the field inside a Batched writes

try {
    final post = await firestore
        .where('active', isEqualTo: true)
        .then((QuerySnapshot snapshot) {
            //Here we get the document reference and return to the post variable.

    var batch = firestore.batch();
    //Updates the field value, using post as document reference
    batch.update(post, { 'someValue': 'newValue' }); 
} catch (e) {