0

I have a cloud firestore collection which am trying to reference in a stream builder like this:

stream:Firestore.instance.collection('htOne').document('path1')
.collection('path2').document('hats').collection('hat')    
.where("${curntUser.isNotEmpty?curntUser:'me'}",isEqualTo: true).orderBy('timesent',descending: true).limit(1000).snapshots(),

The filed for the where clause is going to vary depending on the individual using the app.This would be different for everyone, which means that i'd have to create a composite key every time this filed is created and may not even know this field. Is there a way to get around this?

Norbert
  • 6,874
  • 14
  • 40
  • 65

1 Answers1

1

It looks like you have this style of field in your documents:

"users": {
  "uid1": true,
  "uid3": true,
  "uid4": true,
  "me": true
}

This used to be the recommended way to store a set of values (UIDs in your case), but that is no longer true. In fact, precisely because of the problem you are running into, Firestore now supports using arrays to store such sets of values. The equivalent in your case would be:

"users": ["uid1", "uid3", "uid4", "me"]

And you can then use the array-contains operation in your query:

.where("users", "array-contains", "${curntUser.isNotEmpty?curntUser:'me'}")

This operator was added in version 0.8 of FlutterFire, and is the solution to your problems.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807