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

Firebase No properties to serialize found on class

我在创建 Firebase 数据库时遇到了麻烦。

我正在尝试建模 class。一个很简单的class:

package com.glups.model;

import com.google.firebase.database.IgnoreExtraProperties;

@IgnoreExtraProperties
public class AlumnoFB {

    private String nombre;
    private String apellidos;
    private String telefono;
    private String email;
    private Boolean tieneWhatsapp;
    private Boolean tieneTelegram;
    private Boolean tieneHangouts;
    private Long formaPago;
    private Double ratioHora;
    private Double precioHora;
    private Double horasCompensadas;

    public AlumnoFB() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public AlumnoFB(String nombre,
                    String apellidos,
                    String telefono,
                    String email,
                    Boolean tieneWhatsapp,
                    Boolean tieneTelegram,
                    Boolean tieneHangouts,
                    Long formaPago,
                    Double ratioHora,
                    Double precioHora,
                    Double horasCompensadas) {
        this.nombre = nombre;
        this.apellidos = apellidos;
        this.telefono = telefono;
        this.email = email;
        this.tieneWhatsapp = tieneWhatsapp;
        this.tieneTelegram = tieneTelegram;
        this.tieneHangouts = tieneHangouts;
        this.formaPago = formaPago;
        this.ratioHora = ratioHora;
        this.precioHora = precioHora;
        this.horasCompensadas = horasCompensadas;
    }
}

这几乎就像来自 Firebase 的示例 class。

从 getUser() 获取的应用程序用户已登录 Firebase。

当我调用 SetValue 时:

AlumnoFB alumno = new AlumnoFB("", "", "", "", false, false, false, ((Integer)FormaPago.INT_NO_PAGADO).longValue(), 0.0, 0.0, 0.0);
    mDatabase.child("AlumnoFB").child(ControlClasesFirebase.getUser().getUid()).setValue(alumno) ;

引发致命异常。

06-10 10:17:37.179 13841-13841/com.glups.controlclases E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.glups.controlclases, PID: 13841 com.google.firebase.database.DatabaseException: No properties to serialize found on class com.glups.model.AlumnoFB
at com.google.android.gms.internal.zzaix$zza.<init>(Unknown Source)
at com.google.android.gms.internal.zzaix.zzj(Unknown Source)
at com.google.android.gms.internal.zzaix.zzaw(Unknown Source)
at com.google.android.gms.internal.zzaix.zzav(Unknown Source)
at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
at com.glups.controlclases.MainActivity.onClick(MainActivity.java:305)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5258)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我检查了类型,所有类型都被接受了。怎么了?

Firebase 要求您的 Pojo 具有 public 变量或 getter/setter.

将变量声明更改为 public

public String nombre;
public String apellidos;
public String telefono;
public String email;
public Boolean tieneWhatsapp;
public Boolean tieneTelegram;
public Boolean tieneHangouts;
public Long formaPago;
public Double ratioHora;
public Double precioHora;
public Double horasCompensadas;

如果您使用混淆器,根据您的配置,模型中的某些方法可能会被删除。正如我们所知,POJO 没有太多优化,因为它只有带有 getters and/or(可选)setters 的字段,您可以使用注释“@Keep”,这样混淆器就不会删除此 [=17= 中的任何方法].

检查此以获取更多信息:https://developer.android.com/studio/build/shrink-code.html

@Keep
public class Store {}

在我的例子中,我忘记添加混淆规则来保留模型 类:

-keep class com.google.firebase.example.fireeats.model.** { *; }

这与@aselims 的回答相同,只是 proguard 版本。

我在官方firestore示例中找到的:

https://github.com/firebase/quickstart-android/blob/master/firestore/app/proguard-rules.pro

我今天遇到了这个问题,只需为私有变量提供 getter/setter 即可解决此问题。

例如:

private String pid;

public String getPid() {
    return pid;
}

public void setPid(String pid) {
    this.pid = pid;
}

现在它运行完美,没有任何错误。希望对开发新手有所帮助

将变量声明为 public。

public String email

万一有人遇到这个问题并且 none 上述解决方案有效,声明的变量也需要是 Object 类型而不是原始类型。

例如,您可以使用Integer,但不能使用int

booleanfloatdouble等也是如此。基本上,任何未装箱的类型。

添加

@ServerTimeStamp
public Date date; 

帮我解决了问题

你可以做这两件事中的任何一件 -

  1. 创建你的变量public 或者
  2. 为所有变量添加 getter 和 setter

要解决下面提到的问题,您应该将 class 声明为 Serializable 并且对于代码:

`data class Review(
    var rating:Float=0f,
    var userId:String="",
    var textR:String="",
    @ServerTimestamp var timestamp: Date? = null
 ):Serializable{
constructor( rating: Float,userId: String, text: String):this() {
    this.userId = userId
    this.rating = rating
    this.textR = text
}
}`

解决方案

就我而言,问题仅出现在 release/signed 模式 APK 中。所以我通过执行这些步骤修复了..

1- 添加所有模型 classes 到 proguaed 规则如

-keep class com.YOUR_PACKAGE_NAME.modelClass.** { *; }

@Keep
public class modelClass {}

2- 将模型 class 的所有变量声明为 private

3- 删除了冲突键

镇痛丸:)

验证这一点:

buildTypes {
    release {
        minifyEnabled false
        ...
    }
    debug {
        minifyEnabled false
        ...
    }
}

试试这个解决方案!

1- 实现 Serializable 接口

class model implements Serializable {}

2- Class 具有构造函数和 public setter 和 getter 方法的私有属性的模型

public class Message implements Serializable {

@ServerTimestamp
private Date created;

private String content;
private String from;
private String to;

public Message(Date created, String content, String from, String to) {
    this.created = created;
    this.content = content;
    this.from = from;
    this.to = to;
}

public Date getCreated() {
    return created;
}

public void setCreated(Date created) {
    this.created = created;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getFrom() {
    return from;
}

public void setFrom(String from) {
    this.from = from;
}

public String getTo() {
    return to;
}

public void setTo(String to) {
    this.to = to;
}

}

3- 按照上述 here

在 Proguard 规则中添加此代码
    # Add this global rule
-keepattributes Signature

# This rule will properly ProGuard all the model classes in
# the package com.yourcompany.models.
# Modify this rule to fit the structure of your app.
-keepclassmembers class com.yourcompany.models.** {
  *;
}