由于某些原因,无法从Firebase响应中反序列化对象 [英] For some reason cant deserialize object from Firebase response

查看:70
本文介绍了由于某些原因,无法从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)

多数民众赞成在响应的代码

Thats the code of the response

 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)

推荐答案

Realtime Database(以及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数据类不提供默认的no-arg构造函数,以确保其所有字段都具有初始值.您可以通过为空值或其他一些值作为默认值来告诉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生成默认的无参数构造函数.它还将为每个变量生成setter方法.请注意,每个属性均为var,并提供默认的null值.

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.

如果这不是您想要的数据类外观,则将无法使用自动反序列化.您将必须从快照中读取每个值,确保每个值都不为null,然后将其全部传递给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天全站免登陆