kotlin - Kotlin:将当前日期存储在 Firestore 中作为时间戳
问题描述
不知道为什么我找不到我要找的东西。但我只想将当前时间存储到这样的 Firestore 文档中:
. 在 Flutter 中,我们只是这样做:
'timestamp': DateTime.now()
我尝试在 kotlin 中这样做:
"timestamp" to LocalDateTime.now()
但它给了我一些复杂的领域:
解决方案
在所有平台上最好的方法是使用 SDK 提供的服务器时间戳令牌(而不是使用客户端的时钟,这可能是错误的)。
文档在这里。
// Update the timestamp field with the value from the server
val updates = hashMapOf<String, Any>(
"timestamp" to FieldValue.serverTimestamp()
)
docRef.update(updates).addOnCompleteListener { }
如果您确实需要客户端时钟的时间,只需传递 java Date withDate()
或Timestamp with Timestamp.now()
。
推荐阅读
- sql - 字符串字段在结果中的 20 个字符后被截断
- django - 如何使用 django 模型将多个数据保存到一张表中?
- mysql - 当前时间,但为昨天
- python - pyqt5 标签可见性未隐藏的问题
- flutter - sticky_header:RenderStickyHeader 不满足其约束
- react-native - 将 RN 项目从 0.46.4 升级到 0.60.0 并在 VSCode 中运行结果无法从任何承诺中获得履行价值,所有承诺都被拒绝
- amazon-web-services - AWS Batch 中如何使用 Docker 容器?最大的 vCPU 究竟是多少?
- reactjs - 我没有读到属性绑定
- python-3.x - `python -m venv foo` 不在嵌套环境中安装 pip
- r - ggplot2 PDF barplot 有白线,需要大量放大并且看起来不同。在 PowerPoint 上