简体   繁体   English

实时数据库未更新 // Firebase Kotlin 中的数据未更新

[英]Realtime Database not updating // Firebase Data not Updating in Kotlin

This is the problem:这就是问题:
这就是问题

These are the rules I set in Firebase:这些是我在 Firebase 中设置的规则:
这些是我在 Firebase 中设置的规则

The Current users in Firebase: Firebase当前用户:
Firebase 中的当前用户

When I make a new user, It should be updated in Firebase Realtime DB.当我创建一个新用户时,它应该在 Firebase 实时数据库中更新。 But in this case, this does not help.但在这种情况下,这无济于事。 I even changed the read, write rules to true, it doesn't help & keeps showing null.我什至将读取、写入规则更改为 true,它没有帮助并一直显示 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// //用户Class密码//

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
   }


 }

Check data is successfully saved to the database or not检查数据是否成功保存到数据库

In your addUserToDatabase function add this code to check the status在您的 addUserToDatabase function 添加此代码以检查状态

mDbRef.child("user").child(uid).setValue(User(name, email, uid)).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()){
                    Toast.makeText(this, "Data Added Successfully", Toast.LENGTH_SHORT).show();
                } else { 
                    Toast.makeText(this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    Log.d(TAG, task.getException().getMessage());
                }
            }
        });

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?如果您既没有收到完成也没有收到错误侦听器,那么您是否在首次访问控制台中的数据库之前下载了 google-services.json? In that case, the file might not contain the database reference that the SDK needs.在这种情况下,该文件可能不包含 SDK 需要的数据库引用。 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().要解决此问题,请从您的项目中下载一个新的 google-service.json 并在您的应用程序中使用它,或者在您的代码中的 mDbRef=FirebaseDatabase.getInstance("URL here").getReference() 中指定数据库 URL。

This Solution by @Frank van Puffelen worked. @Frank van Puffelen 的这个解决方案奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM