android - Firebase Realtime Database 不会 store 用户

我正在制作一个 Tinderr 克隆,我想 store Firebase 的实时数据库中的用户。我遵循了文档和多个 youtube 视频,但没有任何效果。用户确实存储在身份验证中,那么为什么不存储在数据库中呢?

注册活动:

package com.example.dogsharev2.activities

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.example.dogsharev2.R
import com.example.dogsharev2.util.DATA_USERS
import com.example.dogsharev2.util.User
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.activity_signup.*

class SignupActivity : AppCompatActivity() {
    private val firebaseDatabase = FirebaseDatabase.getInstance().reference
    private val firebaseAuth = FirebaseAuth.getInstance()
    private val firebaseAuthListener = FirebaseAuth.AuthStateListener {
        val user = firebaseAuth.currentUser
        if(user != null) {
            startActivity(MainActivity.newIntent(this))
            finish()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_signup)
    }

    override fun onStart() {
        super.onStart()
        firebaseAuth.addAuthStateListener(firebaseAuthListener)
    }

    override fun onStop() {
        super.onStop()
        firebaseAuth.removeAuthStateListener(firebaseAuthListener)
    }

    fun onSignup(v: View) {
        if(!emailET.text.toString().isNullOrEmpty() && !passwordET.text.toString().isNullOrEmpty()) {
            firebaseAuth.createUserWithEmailAndPassword(emailET.text.toString(), passwordET.text.toString())
                .addOnCompleteListener { task ->
                    if(!task.isSuccessful) {
                        Toast.makeText(this, "Signup error ${task.exception?.localizedMessage}", Toast.LENGTH_SHORT).show()
                    } else {
                        val email = emailET.text.toString()
                        val userId = firebaseAuth.currentUser?.uid ?: ""
                        val user = User(userId, "", "", email, "", "", "")
                        firebaseDatabase.child(DATA_USERS).child(userId).setValue(user)
                    }
                }
        }
    }

    companion object {
        fun newIntent(context: Context?) = Intent(context, SignupActivity::class.java)
    }
}

数据.kt

package com.example.dogsharev2.util

data class User(
    val uid: String? = "",
    val name: String? = "",
    val age: String? = "",
    val email: String? = "",
    val gender: String? = "",
    val preferredGender: String? = "",
    val imageUrl: String? = ""
)

约束.kt

ackage com.example.dogsharev2.util

val DATA_USERS = "Users"

我也实现了 'com.google.firebase:firebase-database-ktx:20.0.5'。并将我的数据库的规则设置为 true。

提前致谢

回答1

我已经对其进行了多次分析,一切似乎都很好。您可以做的是向 setValue(user) 方法添加一个侦听器,如下所示:

firebaseDatabase.child(DATA_USERS).child(userId).setValue(user)
     .addOnCompleteListener { task ->
            if(task.isSuccessful) {
                 Toast.makeText(this, "Successful", Toast.LENGTH_SHORT).show()
                 } else {
                 Toast.makeText(this, task.getException().getMessage(),
                         Toast.LENGTH_SHORT).show();
                }
      }

现在 task.getException().getMessage() 将让您知道保存用户数据的实际问题。

相似文章

最新文章