提问者:小点点

如何从Firestore文档中获取对象数组


我在Firestore中有一个如下所示的数据结构:

父POJO是:

public class Restaurant {
    private Double distance;
    private GeoPoint geoPoint;
    private int distributionType;
    private List<DailyHours> dailyHours;


public Restaurant(Double distance, GeoPoint geoPoint, int distributionType, List<DailyHours> dailyHours) {

        this.distance = distance;
        this.geoPoint = restaurantLogo;
        this.restaurantDescription = distributionType;
        this.dailyHours = dailyHours;
}

// Getter & Setter (excluded most for the sake of clarity
public List<DailyHours> getDailyHours() {
        return dailyHours;
    }

    public void setDailyHours(List<DailyHours> dailyHours) {
        this.dailyHours = dailyHours;
    }
}

然后是DailyHours POJO:

public final class DailyHours {
    boolean selected;
    String thisDay;
    int startHour;
    int closeHour;

    public DailyHours(boolean selected, String thisDay, int startHour, int closeHour) {
        this.selected = selected;
        this.thisDay = thisDay;
        this.startHour = startHour;
        this.closeHour = closeHour;
    }
// Setter and Getter
}

我如何将它放入一个数组中(其中会有一个数组)?

我正在尝试这样做(其中restaurantArrayList是Restaurant对象的数组):

db.collection("database").get().addOnSuccessListener(queryDocumentSnapshots -> {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                Restaurant restaurant = documentSnapshot.toObject(Restaurant.class);
                restaurantArrayList.add(restaurant);
            }
}

并最终得到以下错误:

无法反序列化对象。 类com.eataway.partner.Models.DailyHours未定义无参数构造函数。 如果您正在使用ProGuard,请确保这些构造函数没有被剥离(在字段'daily hours.[0]'中找到)

我想我必须解析对象数组,但我不知道如何做。 请指教。


共1个答案

匿名用户

错误告诉您Firestore要求对象具有无参数构造函数,以便能够实例化对象进行反序列化。 无参数构造函数听起来就是这样:

public final class DailyHours {
    public DailyHours() {}

    // the rest of your class here
}