将文档添加到 Firestore 时,不会跳过空值。 出于这个原因,我尝试使用 gson 序列化对象。
val gson = GsonBuilder()
.create()
return gson.fromJson(
gson.toJson(this), object : TypeToken<HashMap<String, Any>>() {}.type
)
这很好用,因为我的对象中有一个 Date
类型的变量。将序列化对象添加到我的 Firestore 时,日期变量将其类型更改为 String
,但我要求它的类型为时间戳。如果我不使用 json 序列化对象,则类型符合我的要求,但包括所有值,即使它们为空。