I have such a code to check if value already exists in firebase database:
private boolean nameIsValid(final String input) {
final boolean[] valid = {true};
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot:snapshot.getChildren()) {
if(dataSnapshot.child(input).exists()) {
valid[0] = false;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
return valid[0];
}
It should return true if value does not exist and return false otherwise. But, unforchunatelly, it does not work.