类缺少无参数的构造函数-但我提供了一个构造函数 [英] Class is missing a constructor with no arguments - but I've provided a constructor

查看:144
本文介绍了类缺少无参数的构造函数-但我提供了一个构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Firebase登录后检索用户的信息.我偷偷地怀疑这个错误实际上不是我的问题-并且必须处理这样一个事实,即我正在使用ServerValue.Timestamp为用户注册后的用户存储日期/时间(如我正在尝试的那样)将dateJoined拉回去,而我的班级不知道该怎么做).我的数据库看起来像这样:

I am trying to retrieve the user's info after they log in from Firebase. I have the sneaking suspicion that this error isn't actually my problem - and has to deal with the fact that I'm using ServerValue.Timestamp to store the date/time for a user when they've registered (as I'm trying to pull dateJoined back out, and my class has no idea what to do with it). My database looks something like this:

在登录时,这是我用来获取用户信息的代码

And when logging in, this is the code I use to grab the user's information

//grab user's name from firebase
//drill down to specific user
val usersRef = FirebaseDatabase.getInstance().getReference("Users")
val loggedInUserRef = usersRef.child(user.uid) //specific to the logged in user
val dataRef = loggedInUserRef.child("UserInfo") //get logged in user info
//attach listener
dataRef.addListenerForSingleValueEvent(object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        // Get Post object and use the values to update the UI
        val user_data = dataSnapshot.getValue(User::class.java)
        Log.w(TAG, dataSnapshot.value.toString())

        //add relevant data to shared preferences
        prefs.userName = "$user_data.FirstName $user_data.LastName"
    }

    override fun onCancelled(databaseError: DatabaseError) {}
})

在这里,我的User类位于一个单独的文件中:

And I have my User class in a separate file here:

package androidproject.project_rc

    data class User(
            var FirstName: String = "",
            var LastName: String = "",
            var Email: String = "",
            var Password: String = "",
            var DateJoined: Any
    )

围绕这一行:val user_data = dataSnapshot.getValue(User::class.java)是引发错误的地方-产生了什么?

Around this line: val user_data = dataSnapshot.getValue(User::class.java) is where the error gets thrown - what gives?

推荐答案

看看文档使用snap.getValue(Class)getter时,该类的参数构造函数应为0.

When using the snap.getValue(Class) getter, the class should have a 0 argument constructor.

不幸的是,您不能以这种方式使用数据类.只需将您的班级改写为

So unfortunatly you can't use a data class for that way. Just rewrite your class as

class User(){
    var firstName: String = ""
    var lastName: String = ""
    var email: String = ""
    var password: String = ""
    var dateJoined: Long = 0L
}

按照惯例,您可能要使用小写的变量名. 另外,您可以在dateJoined中避免使用Any,因为我认为这是UTC时间戳.

You might want to use lower case variable names, as it is convention. Also, you can avoid Any for dateJoined, as I assume it is a UTC timestamp.

然后,firebase sdk将检查每个数据库密钥是否存在该名称的setter/public字段,如果存在,则进行设置. Kotlin会自动生成setter和getter,因此您只需要确保成员的名称与数据库中的键相同即可.

The firebase sdk will then check for each of the database keys if a setter/public field for that name exists and if so, set it. Kotlin generates setters and getters automaticly, so you only have to make sure that the members are named the same as the keys in your database.

即使其中一位开发人员曾经说过(而且似乎未出现在文档中),SDK也会尝试访问多种命名样式.这是相当模糊的,因此最好确保匹配数据库密钥.

Even though one of the developers once said (and this seems to not appear in the docs) that the sdk will try to access multiple nameing styles. This is quite vague, so better make sure to match the database keys.

这篇关于类缺少无参数的构造函数-但我提供了一个构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆