flutterfirebasegoogle-cloud-firestore

Flutter cloud_firestore Filter.or using DocumentReference throws Invalid argument error


We're using the cloud_firestore Flutter package (just upgraded to 4.9.3) and we're getting an error when using Filter.or with arrayContains: documentReference. The specific error message is Invalid argument: Instance of '_JsonDocumentReference'

Here's the query:

.where(
  Filter.and(
    Filter(PostKeys.type, whereIn: [PostType.creation.name, PostType.combination.name]),
    Filter(PostKeys.visibility, isEqualTo: ContentVisibility.public.name),
    Filter.or(
      Filter(PostKeys.ingredients, arrayContains: itemReference),
      Filter(PostKeys.tags, arrayContains: itemReference)
    )
  )
)
.orderBy(PostKeys.created, descending: true)
.limit(queryLimit);

RESULT: Invalid argument: Instance of '_JsonDocumentReference'

Even if we remove the Filter.or and instead just include one of the statements in the Filter.and, we get the same error:

.where(
  Filter.and(
    Filter(PostKeys.type, whereIn: [PostType.creation.name, PostType.combination.name]),
    Filter(PostKeys.visibility, isEqualTo: ContentVisibility.public.name),
    Filter(PostKeys.ingredients, arrayContains: itemReference),
  )
)
.orderBy(PostKeys.created, descending: true)
.limit(queryLimit);

RESULT: Invalid argument: Instance of '_JsonDocumentReference'

The changelog for cloud_firestore version 4.9.2 state "FIX(firestore): allow DocumentReference to be used to in Filter queries", but it doesn't appear to be working and we're not sure why.

As a reference, we're running the same query in our Prototype environment using Javascript and it works fine:

pq = query(collection(db, "posts"), 
  and(
    where('type', "in", ['creation','combination']),
    where("visibility", "==", CONSTANTS.visibility.public), 
    or(
      where('ingredients', "array-contains", itemRef), 
      where('tags', "array-contains", itemRef)
    ) 
  ), 
  orderBy("created","desc"), 
  limit(queryLimit)
);

RESULT: Works fine in Javascript (WebV9)


Solution

  • RESOLVED

    I was able to get the query to work. Based on @Frank van Puffelen's comment on my original question, I checked the initialization of the DocumentReference and it was final itemReference = _itemsCollection.doc(item.reference.id);. Instead of that, I just passed the existing item.reference into the query and it worked. Not sure why that made a difference, but it did.