I have an android app that is connected to a Firebase cloud database. For a given user, the database stores a list of Reservation objects.
I followed Google's guidelines to store reservations into the database:
private void addReservation() {
reservations = users.child(mReservation.getUserId()).child(KEY_RESERVATIONS);
reservations.push().setValue(mReservation);
}
The push is successfully saved and my JSON data for a given user has the following format:
{
"email" : "fake.email@fake.com",
"reservations" : {
"-KMTRX1cOAu3v_pN1GHs" : {
"date" : "01/07/2018",
"formattedDate" : "01//0/7/2018",
"formattedSlot" : "10:/0",
"massage" : "Back massage",
"price" : 60,
"promoCode" : false,
"slot" : "10:00",
"userId" : "YD7uQGwApLe5Xi90BSGQhcOqSk32"
}
}
}
When I launch a "ReservationsActivity", I load a list of reservations from the database using this function:
if (userHasReservations(dataSnapshot)) {
DataSnapshot snapshot = dataSnapshot.child(currentUser.getUid()).child(KEY_RESERVATIONS);
List<Reservation> userReservations = snapshot
.getValue(new GenericTypeIndicator<ArrayList<Reservation>>() {});
}
But I get a Database error. The function expected a map:
DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap
According to Google's documentation, if you store elements using the push method, you can then retrieve them as a list. However, I keep running into this mistake.
Can you help me to figure this out?
Thank you very much.