rjsingh12123
rjsingh12123

Reputation: 15

getting value in documentSnapShot but when converting it toobject the object return null,why?

I am using Firestore in my app. when I fetch data from that database, I get data into documentSnapShotobject but when I convert documentSnapShot to a custom object, it gets initialized but it does not contain any data in it.

My Firebase Firestore DB:

1

my POJO classes: class FirebaseData

public class FirebaseData {

    private List<Group> group = null;

    /**
     * No args constructor for use in serialization
     * 
     */
    public FirebaseData() {
    }

    /**
     * 
     * @param group
     */
    public FirebaseData(List<Group> group) {
        super();
        this.group = group;
    }

    public List<Group> getGroup() {
        return group;
    }

    public void setGroup(List<Group> group) {
        this.group = group;
    }

}

class Group

public class Group {

    private String name;
    private List<Sticker> stickers = null;
    private Integer id;

    /**
     * No args constructor for use in serialization
     * 
     */
    public Group() {
    }

    /**
     * 
     * @param name
     * @param stickers
     * @param id
     */
    public Group(String name, List<Sticker> stickers, Integer id) {
        super();
        this.name = name;
        this.stickers = stickers;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Sticker> getStickers() {
        return stickers;
    }

    public void setStickers(List<Sticker> stickers) {
        this.stickers = stickers;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

}


class Sticker

public class Sticker {

    private String name;
    private String id;

    /**
     * No args constructor for use in serialization
     * 
     */
    public Sticker() {
    }

    /**
     * 
     * @param name
     * @param id
     */
    public Sticker(String name, String id) {
        super();
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}


MainActivity class:

db.collection("PackGroups").document("1").get()
                    .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    Log.d("TAG : firebase" ,"onSuccess: "+documentSnapshot.getData());
                    FirebaseData firebaseData = documentSnapshot.toObject(FirebaseData.class );
                    recyclerView.setAdapter(new AdapterGroupPack(MainActivity.this, firebaseData.getGroup()));
                    recyclerView.getAdapter().notifyDataSetChanged();

                }
            });

Upvotes: 0

Views: 523

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

The problem in your code is that the List<Group> group filed is declared in your FirebaseData class starting with a lowercase while in your database is with an uppercase (Group). In order to make it work, both names must match. To solve this, you either change the name of that array in the database to start with a lowercase or you add in front of the getter, the following annotation:

@PropertyName("Group")
public List<Group> getGroup() {
    return group;
}

Upvotes: 1

Related Questions