I've read several articles here on StackOverflow asking this same question and the answers are all the same but it doesn't seem to work for me and I'm not sure why. I've read Elegant Way To Dynamically Query FireStore, Conditional Where Query on Firestore, the two articles that question references, plus How to conditionally add another filter to a query in Firebase Firestore using swift?, and the article Firestore Query Options Swift.
I followed the simple guidance the all offered:
var ref = db.collection("Violations")
if locationCompoundQuery != "" {
ref = ref.whereField("location", isEqualTo: locationCompoundQuery)
}
The issue is that Xcode gives me an error: Cannot assign value of type 'Query' to type "CollectionReference" insert 'as! CollectionReference'. If I do that it will compile and run, but when the query runs it crashes. The message in the console is: Could not cast value of type 'FIRQuery' (0x108972150) to 'FIRCollectionReference'
I'm using Xcode 13.1. All of my other queries that are compound without conditional statements run just fine. Any help is greatly appreciated.