Home > Net >  Get parent of DataSnapShot
Get parent of DataSnapShot

Time:11-07

I have a code to get the node of the child an it come out like this

{YAlthobiti: {firstName: yara, lastName: A, password: 12345, gender: true, DoB: 1/1/2000, email: yara@gmail.com}}

So I need to get the parent (YAlthobiti) to do function on it How can I do it?

this is my code

static Future<String> getUserEmotivID(String email) async{
    String EmotivID = "";

    await _ref.child('Users')
    .orderByChild("email")
    .equalTo(email)
    .once()
    .then((DataSnapshot dataSnapshot) {
      EmotivID =  dataSnapshot.value.toString();
      print(EmotivID);
      return EmotivID;
    }).catchError((error, stackTrace) {
      print("inner: $error");
      print("Dose not Exist");
    });
    return "";
  }

I have searched a lot and found nothing

CodePudding user response:

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

Your code needs to handle this list, by looping over dataSnapshot.value.values

CodePudding user response:

I guess you need the key of that node. You can use the attribute key on your DataSnapshot, as documented here.

So your code would look like this:

.then((DataSnapshot dataSnapshot) {
  EmotivID =  dataSnapshot.key;
  print(EmotivID);
  return EmotivID;
})
  • Related