Android 由于某些原因,无法从Firebase响应中反序列化对象

Android 由于某些原因,无法从Firebase响应中反序列化对象,android,firebase,firebase-realtime-database,kotlin,Android,Firebase,Firebase Realtime Database,Kotlin,一切看起来都很好。需要帮助查找代码中的错误。 由firebase中的快照日志创建 DataSnapshot { key = SecondGame, value = {background=https://firebasestorage.googleapis.com/v0/b/fantasygameapp11.appspot.com/o/background_black.jpg?alt=media&token=b3ec1477-6b52-48

一切看起来都很好。需要帮助查找代码中的错误。 由firebase中的快照日志创建

DataSnapshot
        { key = SecondGame,
                value = {background=https://firebasestorage.googleapis.com/v0/b/fantasygameapp11.appspot.com/o/background_black.jpg?alt=media&token=b3ec1477-6b52-48b4-9296-f57f63f26837, description=SecondGameDescription, tag=https://firebasestorage.googleapis.com/v0/b/fantasygameapp11.appspot.com/o/hot_icon.png?alt=media&token=65516b45-1aca-4cac-9a39-3eddefffe499, 
            title=SecondGame, type=regular} }
这就是模型

data class GameUnit (val background: String, val description: String, val tag: String, val title: String,  val type: String)
这就是响应的代码

 mReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            GameUnit post = dataSnapshot.getValue(GameUnit.class);
        }
我知道可能已经有人问过了,但我需要首先找到问题所在。问题是否也可能是模型在Kotlin中,而firebase响应在Java中

错误

com.google.firebase.database.DatabaseException: Class com.model.GameUnit does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@17.0.0:552)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@17.0.0:545)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@17.0.0:415)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@17.0.0:214)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@17.0.0:79)
    at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@17.0.0:212)
    at com.adultgaming.fantasygameapp.utils.FirebaseManager$1.onDataChange(FirebaseManager.java:47)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)

实时数据库(以及云Firestore)Android SDK附带的反序列化程序要求传递给它的类看起来像JavaBean类型的对象。这意味着它必须有一个默认的no arg constructor(从错误消息中可以看出),以及映射到每个数据库字段的setter方法

Kotlin数据类不提供默认的无参数构造函数,以确保其所有字段都具有初始值。您可以告诉Kotlin,所有的字段都可以没有初始值,方法是将null或其他值作为默认值:

data class GameUnit (
    var background: String = null,
    var description: String = null,
    var tag: String = null,
    var title: String = null,
    var type: String = null
)
对于上述数据类,Kotlin将为Firebase SDK生成一个默认的无参数构造函数。它还将为每个变量生成setter方法。请注意,每个属性都是
var
,并提供默认的空值


如果这不是您希望数据类的外观,那么您将无法使用自动反序列化。您必须从快照中读取每个值,确保它们都不为null,并将它们全部传递给Kotlin提供的构造函数。

您的代码有什么问题?有错误消息吗?哦,是的。对不起,我会的add@DougStevenson添加了错误消息,这样创建带有getter和setter的常规数据java类就更容易了?随你喜欢。Kotlin将生成一个比较所有字段的equals方法,而Java不会。我正在学习Kotlin。现在,我们更愿意确保java中的所有内容都正确无误,而不是手动转换到kotlin。但现在我创建了Java类,但遇到了同样的问题。有没有可能是响应有问题?我不知道-我看不到你的代码。听起来你应该用新代码问一个新问题,但新代码的工作方式与你期望的不一样。啊,不。我已经检查了日志。还是不明白问题出在哪里。响应后的值正是我所需要的,它100%适合Java类模型。但还是有问题