0

I am trying to fetch all the conferences that contain current user's uid. I am trying to accomplish it by using OrderByChild($string).(firebaseauth.currentuser.uid) as show below in my codes. In my codes, i fetched the current users specific key in string and then used it to fetch all the data that contain that key.

String MyAssigningID = "...";
Future<void> _fetchUserASSIGNINGIDFromDatabase() async {
DatabaseReference reference = FirebaseDatabase.instance.reference().child("User").child(widget.GroupID).child("Group-Members").child(auth.currentUser.uid);
final DataSnapshot data = await reference.once();
setState(() {
  MyAssigningID = data.value['AssigningID'].toString();
});
}

GetGroupFunction() {
DatabaseReference referenceData = FirebaseDatabase.instance.reference().child("User").child(widget.GroupID).child("Conferences");
referenceData.orderByChild(MyAssigningID).equalTo(auth.currentUser.uid).once().then((DataSnapshot dataSnapshot) {
  Conferencelists.clear();
  var keys = dataSnapshot.value.keys;
  var values = dataSnapshot.value;

  for (var key in keys) {
    Conferencemodals conferencemodals = new Conferencemodals
      (
        values [key]['Title'],
        values [key]['Description'],
        values [key]['StartingTime'],
        values [key]['EndingTime'],
        values [key]['Duration'],
        values [key]['ConferenceID'],
        values [key]['SchoolUpin'],
        values [key]['CreatedDate'],
        values [key]['Published'],
        values [key]['ConferenceDay'],
        values [key]['ConferenceHost'],
    );
    Conferencelists.add(conferencemodals);
  }
  setState(() {
    TotalConferences = Conferencelists.length.toString();
  });
});
}
  • Your current code makes it easy to find all the users for a specific conference. It does not make it easy to find all the conferences for a specific user. To allow that you'll need an additional top-level data structure that maps from UID to conference IDs. See my explanation here for more: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Aug 18 '21 at 18:03
  • Bro, I have everything okay and it's working fine when I write the specific key of the user manually, but when I pass a string there it does work. String being passed: referenceData.orderByChild(MyAssigningID).equalTo(auth.currentUser.uid).once().then((DataSnapshot dataSnapshot) Write Key Manually: referenceData.orderByChild("10245402415").equalTo(auth.currentUser.uid).once().then((DataSnapshot dataSnapshot) – Anonymous Trap Aug 19 '21 at 13:07
  • The `10245402415` requires that you have an index on `10245402415` in order to execute the query server-side. ¯\_(ツ)_/¯ But if you're only interested in why your code doesn't work, edit your question to show the JSON at `referenceData` in your database (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Aug 19 '21 at 13:34

0 Answers0