Firestore <could not =“”deserialize =“”object。=“”failed =“”to =“”convert =“”a =“”value =“”of =“”type .. =“”>

时间:2018-04-18 20:23:01

标签: android firebase google-cloud-firestore

我试图从Objects collection中获取Firestore的列表,如下所示:

Query query = FirebaseFirestore
        .getInstance()
        .collection("objects")
        .limit(10);

    query.addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot snapshot,
            @Nullable FirebaseFirestoreException e) {

        // Handle errors..

        ObjectList = snapshot.toObjects(ObjectModel.class);

        // Update UI..
    }
});

我的对象类:

public class ObjectModel {
    // Some other fields..
    private Double mPosition;

    public ObjectModel() { }

    public Double getPosition() { return mPosition; }

    public void setPosition(Double position) { mPosition = position; }
}

但后来我得到了:

Could not deserialize object. Failed to convert a value of type java.lang.String to double (found in field 'position')

position字段属于Number类型,因此,为什么我会收到String

修改

文件样本:

enter image description here

2 个答案:

答案 0 :(得分:0)

在你的班级中添加:

//--PROPERTIES--
    @SerializedName("field1")
    @Expose
    private String field1;

    @SerializedName("field2")
    @Expose
    private String field2;


    //-----METHOD------
    @Override
    public String toString() {
        return fields1;
    }

    @Override
    public String toString() {
        return fields2;
    }

答案 1 :(得分:0)

正如道格·史蒂文森(Doug Stevenson)所指出的那样,问题在于我不小心将其降级到比最新的15.0.0更低的版本。