0

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.

kirvel
  • 629
  • 1
  • 6
  • 13
  • Does this answer your question? [Check if value exists in firebase DB](https://stackoverflow.com/questions/37910008/check-if-value-exists-in-firebase-db) – Edward Romero Sep 05 '20 at 07:40
  • @EdwardRomero no, unfortunatelly. I think I did everything as this answer says, but it still doesn't work. – kirvel Sep 05 '20 at 09:15
  • Please check the first duplicate answer to see how you can check a user for existence and the second to see how you can use a callback to get the data from Firestore because you cannot return that value as a result of a method. – Alex Mamo Sep 05 '20 at 09:23

0 Answers0