BVB09
BVB09

Reputation: 875

How to get an array of objects from a Firestore document

I have a data structure in Firestore that looks like below:

enter image description here

The parent POJO is:

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;
    }
}

And then the 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
}

How would I put this into an array (which would have an array within it)?

I am trying this (where restaurantArrayList is an array of Restaurant objects):

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

and end up getting this error:

Could not deserialize object. Class com.eataway.partner.models.DailyHours does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped (found in field 'dailyHours.[0]')

I presume I have to parse through the array of objects but I have no idea how to do that. Please advise.

Upvotes: 0

Views: 81

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

The error is telling you that Firestore requires that objects have a no-argument constructor to be able to instantiate objects for deserialization. A no-argument constructor is exactly what it sounds like:

public final class DailyHours {
    public DailyHours() {}

    // the rest of your class here
}

Upvotes: 1

Related Questions