1. 首页
  2. 其他

Firebase – 在类上找不到要序列化的属性

本文介绍了Firebase – 在类上找不到要序列化的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂这样的课:

class dataModel {
    String id, name;
    Integer count;

    dataModel() {}
}

然后我从 Firebase 添加数据.

And I add data from Firebase.

mDatabase.addValueEventListener(mListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            dataSet.add(dataSnapshot.getValue(dataModel.class));
            //...
        }
});

当我以调试方式运行应用程序时,没有问题.但是发布后,应用程序崩溃并显示错误:

When I run the app as debug, there is no problem. But after it is released, app crashes with the error:

com.google.firebase.database.DatabaseException: No properties to serialize found on class com.my.package.dataModel

我有 minifyEnabled true

I have minifyEnabled true

推荐答案

要解决这个问题,您的类需要实现 Serializable 接口:

To solve this, your class needs to implement the Serializable interface:

class dataModel implements Serializable {}

您还需要添加一个带有三个参数的构造函数.你的类应该是这样的:

You also need to add a contructor with three arguments. Your class should look like this:

class dataModel implements Serializable {
    private String id, name;
    private Integer count;
    @ServerTimestamp
    private Date time;

    dataModel() {}

    dataModel(String id, String name, Integer count) {
        this.id = id;
        this.name = name;
        this.count = count;
    }
}

如果日期为 null,它将具有服务器生成的时间戳.所以你不需要为它设置值.另请参阅用于标记要填充服务器时间戳的 Date 字段的 annotation.

If the date is null, it will have the server-generated timestamp. So you don’t need to do set the value for it. Please also see the annotation used to mark a Date field to be populated with a server timestamp.

同样很重要,不要忘记添加public getter.

Also very important, don’t forget to add public getters.

另一个要求是添加 -keepclassmembers class com.yourcompany.models.** { *;} 在 proguard-rules.pro 中.

and the other requirement will be to add -keepclassmembers class com.yourcompany.models.** { *; } in proguard-rules.pro.

正如此处所述.

这篇关于Firebase – 在类上找不到要序列化的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

Firebase – 在类上找不到要序列化的属性 为WP2原创文章,链接:https://www.wp2.cn/other/firebase-%e5%9c%a8%e7%b1%bb%e4%b8%8a%e6%89%be%e4%b8%8d%e5%88%b0%e8%a6%81%e5%ba%8f%e5%88%97%e5%8c%96%e7%9a%84%e5%b1%9e%e6%80%a7/