Home > database >  using two queries to get 'data1' OR 'data2' in flutter firebase
using two queries to get 'data1' OR 'data2' in flutter firebase

Time:10-07

I want to get data from firebase only if the user is not saved in the document. Because I need a or-query, which is not possible in Firebase, I decided to use RxDart's CombinedStreams with two steams:

var streamOne = FirebaseFirestore.instance
  .collection('jobs')
  .where('jobState', isEqualTo: 1)
  .where('userOne', isNotEqualTo: FirebaseAuth.instance.currentUser!.uid)
  .snapshots();
var streamTwo = FirebaseFirestore.instance
  .collection('jobs')
  .where('jobState', isEqualTo: 1)
  .where('userTwo', isNotEqualTo: FirebaseAuth.instance.currentUser!.uid)
  .snapshots();

But my app shows the data even if the current user is in 'userOne' OR 'userTwo'. Is it possible to avoid this and get the data just if the currentUser is not 'userOne' OR 'userTwo'?

CodePudding user response:

Your logic is flawed: if the UID is in userOne, the second query will still return that document, and vice versa. What you want is actually an AND: the documents where the UID is not in userOne and not in userTwo.

Unfortunately though that query also isn't possible on Firestore, as all not-equal conditions in a query must be on the same field.

There is no way to capture your logic in a single query, and you will have to filter the documents fo userOne and userTwo in your application code instead.

  • Related