Java Firestore查询快照返回字符串而不是布尔值

时间:2018-12-21 10:55:36

标签: java android firebase google-cloud-firestore

我正在尝试将查询结果从firestore映射回我的Java应用程序中的类。

我已将其另存为Web控制台上的Boolean,请参见图片:

enter image description here

然后在应用程序上,我使用以下函数将其映射到我的班级:

query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        List<uk.wefix.FirestoreModels.Job> mJobsList = new ArrayList<>();
        if(task.isSuccessful()){
            for(QueryDocumentSnapshot document : task.getResult()) {
                uk.wefix.FirestoreModels.Job job = document.toObject(uk.wefix.FirestoreModels.Job.class);
                mJobsList.add(job);
            }
            mjobsAdaptor.clear();
            mjobsAdaptor.addAll(mJobsList);
        } else {
            Log.d("JobsFragment", "Error getting documents: ", task.getException());
        }
    }
});

它映射到的类:

public class Job {

    public String first_name;
    public String second_name;
    public String phone_number;
    public Boolean complete;

    public Job() {}

    public Job(String first_name, String second_name, String phone_number, Boolean complete) {
        this.first_name = first_name;
        this.second_name = second_name;
        this.phone_number = phone_number;
        this.complete = complete;
    }

    /**
     * @return String
     */
    public String getFirst_name(){
        return first_name;
    }

    /**
     * @return String
     */
    public String getSecond_name(){
        return second_name;
    }

    /**
     * @return String
     */
    public String getPhone_number(){
        return phone_number;
    }

    /**
     * @return Boolean
     */
    public Boolean getComplete(){
        return complete;
    }
}

Firestore似乎返回的是String而不是Boolean,这意味着我收到了以下错误消息:

java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.lang.String to boolean (found in field 'complete')

很显然,我可以在Job类中做类似的事情,但这似乎有点hacky,而且我不确定这仅仅是一个错误还是做错了什么。

潜在的“ hacky”解决方案

/**
 * @return Boolean
 */
public Boolean getComplete(){
    if(complete.equals("true")){
        return true;
    }else{
        return false;
    }
}

查询:

CollectionReference jobs = db.collection("jobs");
Query query = jobs.whereEqualTo("technician_id", 10).whereEqualTo("date", "2018-12-12");

0 个答案:

没有答案