0

I'm having a really hard time understanding how Nosql works so I hope someone can help me understand it a bit better.

I'm trying to make a simple chat application (One to one chat support and chat groups) and want to dislay a list of all the conversations that the current user is in. This is my table for it.

enter image description here

I tried getting the data in several ways. But what I currently have is this (Which should work according to the internet, but doesn't).

_membersRef.equalTo(1508, key: '1508').once().then((DataSnapshot snap) {
            print(snap.value);
        });

I also tried

_membersRef.startAt(1508).endAt(1508).once().then((DataSnapshot snap) {
            print(snap.value);
        });

What I want my code to do is return all records that have my account_id in them (1508 in this case). So it should return the record "one".

So if I change the uid in the code to 1509 it should return "One" and "two". How can I make this happen?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Kevin Walter
  • 6,507
  • 8
  • 28
  • 32

1 Answers1

3

To get the key one try this:

_membersRef.orderByChild('1508').equalTo(true).once().then((DataSnapshot snap) { 
print(snap.value); 
});

the snapshot is at child members then you order it according to child 1508 which is equalTo(true).

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • This still returns all the records but with the correct records on top. But the app should not receive members that are not his. That would be a huge privacy problem. I'm getting this back: {one: {1509: true, 1508: true}, two: {1509: true}} "two" should not be in the received data – Kevin Walter Apr 12 '18 at 11:11
  • try `child("members").orderByChild("1508").equalTo(true)` – Peter Haddad Apr 12 '18 at 11:19
  • Omg I think it works!!!! If you can change your answer code to `_membersRef.orderByChild('1508').equalTo(true).once().then((DataSnapshot snap) { print(snap.value); });` Then I can vote yours as the answer as this is a flutter question and not javascript. But thank you so much! – Kevin Walter Apr 12 '18 at 11:22
  • While this query works, it requires that you have a index defined on `1508`, `1509`, etc. If those child nodes are not pre-defined, that is likely not feasible and you should create an inverted data structure as Günter's answer shows. Also have a look at my answer here: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Apr 12 '18 at 13:44