由于某种原因不能从 Firebase 响应反序列化对象 [英] For some reason cant deserialize object from Firebase response

查看:25
本文介绍了由于某种原因不能从 Firebase 响应反序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切看起来都不错.需要帮助查找代码中的错误.来自 firebase 快照的日志

Everything looks good. Need help in finding a mistake in code. By the logs that is the snapshot from 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 中?

I know it might be already asked but I need to find the issue first of all. Is it also possible the problem is that model is in Kotlin but firebase response in 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)

推荐答案

实时数据库(以及 Cloud Firestore)Android SDK 附带的解串器要求您传递给它的类看起来像 JavaBean 类型对象.这意味着它必须有一个默认的无参数构造函数,正如您从错误消息中可以看出的,还有映射到每个数据库字段的 setter 方法.

The deserializer that comes with the Realtime Database (and also Cloud Firestore) Android SDKs requires that the class you pass to it look like a JavaBean type object. This means that it must have a default no-arg constuructor, as you can tell from the error message, and also setter methods that map to each database field.

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

Kotlin data classes don't provide a default no-arg constructor in order to ensure that all of its fields have an initial value. You can tell Kotlin that it's OK for all of the fields not to have an initial value by giving null or some other value as a default value:

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 使用.它还将为每个 var 生成 setter 方法.请注意,每个属性都是 var 并提供一个默认的空值.

For the above data class, Kotlin will generate a default no-arg constructor for the Firebase SDK to use. It will also generate setter methods for each var. Note that each property is var and provides a default null value.

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

If this is not what you want your data class to look like, you won't be able to use automatic deserialization. You will have to read each value out of the snapshot, make sure they are each not null, and pass them all to the constructor that Kotlin provides.

这篇关于由于某种原因不能从 Firebase 响应反序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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