在Android中使用模型类时,Firestore FieldValue.ServerTimestamp始终为null? [英] Firestore FieldValue.ServerTimestamp always null while using model class in android?

查看:22
本文介绍了在Android中使用模型类时,Firestore FieldValue.ServerTimestamp始终为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他可能会被问过很多遍,但是没有一个解决方案对我不起作用.现在让我告诉你,到目前为止,我只是使用简单的模型类在Cloud Firestore db中保存值,而在那个模型类中我尝试了什么使用了timestamp字段,但是在插入我的timestamp时始终为null,现在不知道在哪里出错了,让我发布我的代码:

Hhis may be asked many times but none of the solutions didn't works for me now let me tell you what I have tried so far am using just simple model class to save values in Cloud Firestore db and in that model class am having using timestamp field but while inserting my timestamp is always null don't know where am making mistake now let me post my code :

 import com.google.firebase.firestore.ServerTimestamp;

    import java.util.Date;

      public class DriverMasterModel {

    private String test;
    @ServerTimestamp
    private Date date;

    public DriverMasterModel() {}

    public DriverMasterModel(String test) { this.test = test; }

    public String getTest() { return test; }
    @PropertyName("Created_At")
    public Date getDate() { return date; }
}

这里是将数据插入db:

and here am inserting the data to db:

DriverMasterModel driverMasterModel=new DriverMasterModel();
                driverMasterModel.setTest("test");
                FirebaseFirestore dbb=FirebaseFirestore.getInstance();
                dbb.collection("Test").add(driverMasterModel).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                    @Override
                    public void onSuccess(DocumentReference documentReference) {

                    }
                });

但是我的Created_at字段在Firestore数据库中始终为空.

But my Created_at field always null in Firestore db.

推荐答案

由于模型类错误,您总是会得到 null .您的 MasterModel 类应如下所示:

You are getting always null because your model class is wrong. Your MasterModel class should look like this:

public class MasterModel {
    private String test;
    @ServerTimestamp
    private Date date;

    public MasterModel() {}

    public MasterModel(String test) { this.test = test; }

    public String getTest() { return test; }

    public Date getDate() { return date; }
}

请参见,类的名称与构造函数的名称相同,并且 Date 对象的名称是简单的 date ,而不是 Created_At .

See, the name of the class is the same as the name of the constructor and the name of the Date object is simple date and not Created_At.

根据您的评论,请查看模型类的新配置:

According to your comment, please see the new configuration of your model class:

public class MasterModel {
    private String test;
    @ServerTimestamp
    private Date createdAt, updatedAt;

    public MasterModel() {}

    public MasterModel(String test) { this.test = test; }

    public String getTest() { return test; }

    public Date getCreatedAt() { return createdAt; }

    public Date getUpdatedAt() { return updatedAt; }
}

这篇关于在Android中使用模型类时,Firestore FieldValue.ServerTimestamp始终为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆