Firestore FieldValue.ServerTimestamp always null w

2019-08-08 00:46发布

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; }
}

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) {

                    }
                });

But my Created_at field always null in Firestore db.

1条回答
家丑人穷心不美
2楼-- · 2019-08-08 01:04

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; }
}

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.

Edit: 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; }
}
查看更多
登录 后发表回答