Home > Software design >  Realtime Database not updating // Firebase Data not Updating in Kotlin
Realtime Database not updating // Firebase Data not Updating in Kotlin

Time:01-08

This is the problem:
This is the problem

These are the rules I set in Firebase:
These are the rules I set in Firebase

The Current users in Firebase:
The Current usersin Firebase

When I make a new user, It should be updated in Firebase Realtime DB. But in this case, this does not help. I even changed the read, write rules to true, it doesn't help & keeps showing null.

//CODE BELOW//

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_sign_up)
    supportActionBar?.hide()

    mAuth = FirebaseAuth.getInstance()
    edtName =findViewById(R.id.edt_name)
    edtEmail =findViewById(R.id.edt_email)
    edtPassword =findViewById(R.id.edt_password)
    btnSignUp =findViewById(R.id.btn_signup)

    btnSignUp.setOnClickListener{
        val name = edtName.text.toString()
        val email = edtEmail.text.toString()
        val password = edtPassword.text.toString()

        signUp(name, email,password)

    }
}
private fun signUp(name:String, email: String,password: String){
    //logic for signup

    mAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                //going homE
                addUserToDatabase(name, email, mAuth.currentUser?.uid!!)

                val intent= Intent(this@SignUp, MainActivity::class.java)
                startActivity(intent)

            } else {
                Toast.makeText(this@SignUp, "Some Error occurred", Toast.LENGTH_SHORT).show()

            }
        }

}
private fun addUserToDatabase(name:String, email:String, uid:String){
    mDbRef=FirebaseDatabase.getInstance().getReference()

    mDbRef.child("user").child(uid).setValue(User(name, email, uid))

}

}

//User Class Code//

package com.example.mychat

class User {
   var name : String? = null
   var email : String? = null
   var uid : String? = null

   constructor(){}

   constructor(name:String?, email:String?, uid:String?){
     this.name= name
     this.email=email
     this.uid=uid
   }


 }

CodePudding user response:

First of all, use data class instead of User model. In data class no need to getter and setter or any type of constructor

User.kt

data class User(
    var name: String? = null,
    var email: String? = null,
    var uid: String? = null
)

Check data is successfully saved to the database or not

In your addUserToDatabase function add this code to check the status

mDbRef.child("user").child(uid).setValue(User(name, email, uid)).addOnCompleteListener {
            if (it.isSuccessful) {
                Toast.makeText(this, "Data Added Successfully", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.exception?.message}", Toast.LENGTH_SHORT).show()
            }
        }

CodePudding user response:

If you're getting neither a completion nor an error listener, did you download your google-services.json before you first accessed the database in the console by any chance? In that case, the file might not contain the database reference that the SDK needs. To fix this, download a new google-service.json from your project and use that in your app, or specify the database URL in your code in mDbRef=FirebaseDatabase.getInstance("URL here").getReference().

This Solution by @Frank van Puffelen worked.

  •  Tags:  
  • Related