英文:
[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
。
正如您在代码中所看到的,首先我读取了这个文档:
(B):calSnapshot
。
然后,在第二个实例中,我读取了另一个文档,该文档在 (A) 中的一个类型为 document
的字段中有引用:
并且我将这个第二个文档映射到了 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:
(B): calSnapshot
.
And then, in the second instance, I read another document that is referred in one field of type document
in (A):
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
。所以没有必要这样做。
编辑:
以下是解决整个问题的步骤:
- 将
lh
属性声明为HashMap<String, Object>
类型 - 移除@Ignore注释
- 将getter的返回类型从void更改为返回
HashMap<String, Object>
- 将
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:
- Declare
lh
property to be of typeHashMap<String, Object>
- Remove the @Ignore annotation
- Change the return type of the getter void to return the
HashMap<String, Object>
- 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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论