I want to create method that will check if a child is present or not in firebase database and returns the value true or false accordingly. here's my code
public boolean check_if_child_exist(DatabaseReference reference, String child){ boolean if_exist; reference.addListenerForSingleValueEvent(new ValueEventListener(){ @Override public void onDataChange(DataSnapshot datasnapshot) { if(datasnapshot.hasChild(child)) { if_exist = true; } } @Override public void onCancelled(DatabaseError error) { if_exist = false; } }); return if_exist; }
I have tried below code but it didn't work
if(datasnapshot.hasChild(child)) { return true;}