有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java[CustomClassMapper]:没有用于。。。在Firebase Firestore的类中找到

我不知道为什么会有这样的警告:

W/Firestore: (21.3.1) [CustomClassMapper]: No setter/field for lh. found on class 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

正如您在代码中看到的,首先我阅读了以下文档:

enter image description here

(B):calSnapshot

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

enter image description here

我将第二个文档映射到Liturgia.class

如果我尝试将该方法命名为set lh()或尝试不命名@PropertyName("lh"),则警告始终显示

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

我看到了这个答案:W/Firestore: [CustomClassMapper]: No setter/field for class Android但在我的例子中,属性的名称和方法的名称是相同的


共 (1) 个答案

  1. # 1 楼答案

    您将收到以下警告:

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

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

    除此之外,使用:

    @PropertyName("lh")
    

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

    编辑:

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

    1. lh属性声明为HashMap<String, Object>类型
    2. 删除@Ignore注释
    3. 更改getter void的返回类型以返回HashMap<String, Object>
    4. 在setter中设置一个HashMap<String, Object>

    所以,这里是我的灵感来源:

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

    对于二传手来说:

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