[CustomClassMapper]: 在Firebase Firestore中的类上找不到对…的setter/field。

huangapple go评论111阅读模式
英文:

[CustomClassMapper]: No setter/field for ... found on class in Firebase Firestore

问题

我不知道为什么会出现这个警告:

> W/Firestore:(21.3.1)[CustomClassMapper]:找不到 lh 的 setter/field。
> 在类 com.example.model.Liturgia 中找到

##获取数据的代码:

    DocumentReference calRef = db.collection(CALENDAR_PATH).document(fechaYY).collection(fechaMM).document(fechaDD);
    calRef.addSnapshotListener((calSnapshot, e) -> {
        if ((calSnapshot != null) && calSnapshot.exists()) {
            mLiturgia = calSnapshot.toObject(Liturgia.class);
            DocumentReference dataRef = calSnapshot.getDocumentReference("lh.1");
            // ...

##模型

public class Liturgia {
    private Breviario lh;
    //...

    public Liturgia() {
    }

    @Exclude
    public void getlh() {
    }

    @PropertyName("lh")
    public void setLh(Breviario lh) {
        this.lh = lh;
    }
}

##文档

(A):calRef

正如您在代码中所看到的,首先我读取了这个文档:

[CustomClassMapper]: 在Firebase Firestore中的类上找不到对…的setter/field。

(B):calSnapshot

然后,在第二个实例中,我读取了另一个文档,该文档在 (A) 中的一个类型为 document 的字段中有引用:

[CustomClassMapper]: 在Firebase Firestore中的类上找不到对…的setter/field。

并且我将这个第二个文档映射到了 Liturgia.class

如果我尝试将方法命名为 set lh(),或者尝试去掉 @PropertyName("lh"),警告总是会显示。

代码按预期工作,但我不想看到这个警告。

我看到了这个答案:https://stackoverflow.com/q/58025961/5587982,但在我的情况下,属性的名称和方法的名称是相同的。

英文:

I don't know why I'm having this warning:

> W/Firestore: (21.3.1) [CustomClassMapper]: No setter/field for lh.
> found on class com.example.model.Liturgia

##Code for getting data:

    DocumentReference calRef = db.collection(CALENDAR_PATH).document(fechaYY).collection(fechaMM).document(fechaDD);
    calRef.addSnapshotListener((calSnapshot, e) -> {
        if ((calSnapshot != null) && calSnapshot.exists()) {
            mLiturgia = calSnapshot.toObject(Liturgia.class);
            DocumentReference dataRef = calSnapshot.getDocumentReference("lh.1");
            // ...

##Model

public class Liturgia {
    private Breviario lh;
    //...

    public Liturgia() {
    }

    @Exclude
    public void getlh() {
    }

    @PropertyName("lh")
    public void setLh(Breviario lh) {
        this.lh = lh;
    }
}

##Documents

(A): calRef.

As you can see in the code, first I read this document:

[CustomClassMapper]: 在Firebase Firestore中的类上找不到对…的setter/field。

(B): calSnapshot.

And then, in the second instance, I read another document that is referred in one field of type document in (A):

[CustomClassMapper]: 在Firebase Firestore中的类上找不到对…的setter/field。

And I map this second document to the Liturgia.class.

If I try to name the method as set lh() or I try without @PropertyName("lh") the warning shows always.

The code works as expected, but I don't want to see this warning.

I see this answer: https://stackoverflow.com/q/58025961/5587982 but in my case, the name of the property and the name of the method are the same.

答案1

得分: 4

你正在收到以下警告:

> W/Firestore:(21.3.1)[CustomClassMapper]:在类com.example.model.Liturgia上未找到lh的setter/field

因为你的Liturgia类中的lh属性是类型为Breviario,而数据库中是字符串数组。两者必须匹配。所以如果你需要保留当前的数据库数据,将该属性更改为List<String>类型。

此外,使用:

@PropertyName("lh")

是无用的,因为数据库中的属性已经被称为lh。所以没有必要这样做。

编辑:

以下是解决整个问题的步骤:

  1. lh属性声明为HashMap<String, Object>类型
  2. 移除@Ignore注释
  3. 将getter的返回类型从void更改为返回HashMap<String, Object>
  4. HashMap<String, Object>设置到setter中。

因此,这是getter:

public HashMap<String, Object> getLh() {
    return lh;
}

而setter是:

public void setLh(HashMap<String, Object> lh) {
    this.lh = lh;
}
英文:

You are getting the following warning:

> W/Firestore: (21.3.1) [CustomClassMapper]: No setter/field for lh found on class com.example.model.Liturgia

Because the lh property in your Liturgia class is of type Breviario, while in the database is an array of Strings. Both must match. So if you need to keep the current database data, change that property to be of type List<String>.

Besides that, using:

@PropertyName("lh")

It is useless because the property in the database is already called lh. So there is no need for that.

Edit:

Here are the steps for solving the entire problem:

  1. Declare lh property to be of type HashMap<String, Object>
  2. Remove the @Ignore annotation
  3. Change the return type of the getter void to return the HashMap<String, Object>
  4. Set a HashMap<String, Object> into the setter.

So, here si the getter:

public HashMap<String, Object> getLh() {
    return lh;
}

And for the setter:

public void setLh(HashMap<String, Object> lh) {
    this.lh = lh;
}

huangapple
  • 本文由 发表于 2020年4月3日 23:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61015574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定