Reputation: 41
I need to return a data from Firebase to a Java Object, but even using an Interface, it always returns me null. Some help?
I created a "Coordinate" object inside the method and I'm trying to access the data like this: readData(value -> coordinates = value , offer);
public interface CoordinatesCallback {
void onCallBack(Coordinates value);
}
public void readData(CoordinatesCallback callback, Offer offer) {
String storeId = Integer.toString(offer.getStore().getId());
mDatabase.child("stores").child(storeId).child("coordinates").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Coordinates coordinates = dataSnapshot.getValue(Coordinates.class);
callback.onCallBack(coordinates);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Upvotes: 0
Views: 253
Reputation: 317467
That's not how getValue() works. You have to pass it the Class object of a Java POJO with getters and setters for each field of the database you want to read. From the linked Javadoc:
This method is used to marshall the data contained in this snapshot into a class of your choosing. The class must fit 2 simple constraints:
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
Upvotes: 1