0

When I use the Update_User_Information() after else if then dataSnapshot.value.toString() shows null value and because of that Update_User_Information() gets executed.

FirebaseDatabase.getInstance().getReference("Users").child(firebaseAuth.uid!!).addListenerForSingleValueEvent(object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        if (dataSnapshot.value.toString().contains(signup_username)) {
            progressDialog.dismiss()
            binding.signupUsername.error = "This username is already registered"
            return
        }
        else if (dataSnapshot.value.toString().contains(signup_phone)) {
            progressDialog.dismiss()
            binding.signupUsername.error = "This Phone Number is already registered"
            return
        }
        Toast.makeText(this@Signup, dataSnapshot.value.toString(), Toast.LENGTH_LONG).show() // shows null
        Update_User_Information()
    }
    override fun onCancelled(error: DatabaseError) {
        Toast.makeText(this@Signup, "There is a problem in database. Please report this issue to support.", Toast.LENGTH_LONG).show()
    }
})

but if I use same code with a litte change then it works but it also executes the

FirebaseDatabase.getInstance().getReference("Users").child(firebaseAuth.uid!!).addListenerForSingleValueEvent(object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        if (dataSnapshot.value.toString().contains(signup_username)) {
            progressDialog.dismiss()
            binding.signupUsername.error = "This username is already registered"
        }
        else if (dataSnapshot.value.toString().contains(signup_phone)) {
            progressDialog.dismiss()
            binding.signupUsername.error = "This username is already registered"
        }

    }

    override fun onCancelled(error: DatabaseError) {
        Toast.makeText(this@Signup, "There is a problem in database. Please report this issue to support.", Toast.LENGTH_LONG).show()
    }
})
Update_User_Information()

What I am trying to do is, checking the username and phone number if it's registered in my database or not, if it's found in my database then I don't want to execute Update_User_Information(). Any solution ?

Here is the image of my databse

Zeltrax
  • 1
  • 1
  • Please edit your question and add your database structure as a JSON file. You can simply get it by clicking the Export JSON in the overflow menu (⠇) in your [Firebase Console](https://console.firebase.google.com/u/0/project/_/database/data) and indicate the exact data you want to check. – Alex Mamo Oct 03 '21 at 09:21
  • @AlexMamo Done, pls check it now – Zeltrax Oct 04 '21 at 10:26
  • So you need to check on both conditions, right? – Alex Mamo Oct 04 '21 at 10:58
  • @AlexMamo, I just want to check user's entered username and phone number whether they are already registered or not. If it's registered then the function Update_User_Information() should not execute but if username and phone number is not found in my database then only the function should be executed. – Zeltrax Oct 04 '21 at 19:21

0 Answers0