【问题标题】:How to convert DateTime to Timestamp before saving in Firestore in Flutter?如何在 Flutter 中的 Firestore 中保存之前将 DateTime 转换为时间戳?
【发布时间】:2020-09-11 15:10:36
【问题描述】:

我选择了一个“日期”表单 datePicker 并希望将该日期(类型为 DateTime)以时间戳类型保存到 Firestore。 我使用了 Firestore.fromDate(date),但它将数据存储在 Firestore 中作为地图(纳秒,秒)。

【问题讨论】:

  • DateTime.now().millisecondsSinceEpoch 将日期存储在 int 中,然后您可以在使用 api.dart.dev/stable/2.8.2/dart-core/DateTime/… 时对其进行解码
  • @GJJ2019 感谢您的帮助,但如果有任何方法可以将日期存储在时间戳中,那会更好,因为我们团队为 React Native 应用程序编写的代码现在太大而无法更改数据类型。

标签: flutter-web


【解决方案1】:

使用 Firebase Timestamp 类作为:

Timestamp.fromMicrosecondsSinceEpoch(myDateTime.microsecondsSinceEpoch)

还有很多其他的构造函数,看看文档。

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 2019-11-30
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2021-11-08
    • 2020-10-09
    • 1970-01-01
    相关资源
    最近更新 更多