【问题标题】:Firebase Firestore : Document is not mapping to Model classFirebase Firestore:文档未映射到模型类
【发布时间】:2020-07-24 21:20:30
【问题描述】:

我尝试使用 Firebase Firestore 构建应用,但在检索到 SnapshotListener 上的数据后,它没有映射到我的模型。

这是我的Model 班级

public class BlogPost {
    private String description;
    private String image;
    private String user_id;
    private String thumbnail_url;
    private Timestamp timestamp;
    public BlogPost() {
    }
    public BlogPost(String description, String image, String user_id, String thumbnail_url, Timestamp  timestamp) {
        this.description = description;
        this.image = image;
        this.user_id = user_id;
        this.thumbnail_url = thumbnail_url;
        this.timestamp = timestamp;
    }

    public String getImageUrl() {
        return image;
    }

    public void setImageUrl(String image) {
        this.image = image;
    }

    public String getUserId() {
        return user_id;
    }

    public void setUserId(String user_id) {
        this.user_id = user_id;
    }

    public String getThumbnailUrl() {
        return thumbnail_url;
    }

    public void setThumbnailUrl(String thumbnail_url) {
        this.thumbnail_url = thumbnail_url;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Timestamp getPostTime() {
        return timestamp;
    }

    public void setPostTime(Timestamp timestamp) {
        this.timestamp = timestamp;
    }

}

要添加数据,我正在使用此代码:

firestore.collection("posts").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()){
                    if (doc.getType() == DocumentChange.Type.ADDED){
                        BlogPost blogPost = doc.getDocument().toObject(BlogPost.class);

                        blogList.add(blogPost);`

                        blogRecyclerAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

这是我映射后的对象

BlogPost{description='post 6', image='null', user_id='null', thumbnail_url='null', timestamp=null}

这是数据库结构截图

如您所见,描述已映射,但所有其他字段均为空。 任何帮助将不胜感激。

【问题讨论】:

  • 请编辑您的问题并将您的数据库结构添加为屏幕截图。也请回复@AlexMamo
  • 我们需要能够看到您正在使用的实际文档数据,因为 Firestore 会尝试使用它们的名称自动将您的文档字段映射到您的 POJO 属性。如果字段的名称与 setter 方法的名称不匹配,则映射将失败。
  • @DougStevenson 数据库字段名称与模型类中的变量完全相同。
  • 虽然我加了截图

标签: java android firebase google-cloud-firestore


【解决方案1】:

需要进行两项更改。

-> 公开数据类中的每个数据成员

-> 用@PropertName() 注释它们

  public class BlogPost {

   @PropertyName("description")
   public String description;
   @PropertyName("image")
   public String image;
   @PropertyName("user_id")
   public String user_id;
   @PropertyName("thumbnail_url")
   public String thumbnail_url;
   @PropertyName("timestamp")
   public Timestamp timestamp;
  }

试试这个。

【讨论】:

  • 公开成员不一定是最好的决定。从设计的角度来看,通常最好使用 getter 和 setter 方法,同时让成员保持私有。这一切都可以通过 Firestore 指定。
猜你喜欢
  • 2018-03-19
  • 2021-09-03
  • 1970-01-01
  • 2018-11-09
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
相关资源
最近更新 更多