【问题标题】:how to get firebase time in firebase如何在firebase中获得firebase时间
【发布时间】:2020-06-04 15:42:21
【问题描述】:

我的应用程序 firebase(服务器)需要毫秒时间。因为我的当地时间将被移动日期时间设置改变,

我会看到这个问题不太有帮助, Flutter Firestore Server side Timestamp

var timestamp=FieldValue.serverTimestamp();

print("timestamp"+timestamp.toString());

但它们的打印值低于值

====>Instance of 'FieldValue'

【问题讨论】:

    标签: datetime flutter time google-cloud-firestore


    【解决方案1】:

    在读取时间戳之前,您必须将其设置到服务器,因为您是从那里生成的。根据this

    serverTimestamp() 返回与 set() 或 update() 一起使用的标记,以在写入的数据中包含服务器生成的时间戳。

    所以先设置一下

    Firestore.instance.collection('timestamp').document('docId').setData({'time' : FieldValue.serverTimestamp()});
    

    然后使用基本的 Firestore 查询获取它。最后,查看this的回答了解详情。

    获取时间戳的更通用方法

        getTimestamp() async{ 
          await Firestore.instance.collection('timestamp').document('docId').setData({'time' : FieldValue.serverTimestamp()});
          DocumentSnapshot doc = await Firestore.instance.collection('timestamp').document('docId').get();
          Timestamp timestamp = doc['time'];
          var timestampInMilliSeconds = timestamp.millisecondsSinceEpoch;
          return timestampInMilliSeconds;
          /* here you can call setState to your own variable before returning and keep in mind this method is async so it will return a Future.  */
        }
    

    【讨论】:

    • 我已经在使用它,但这是根据我的移动(设备)时间设置的。不是服务器。
    • 好的,那你为什么不使用 FiledValue.serverTimestamp() 并立即从 firestore 中获取数据
    • 是的,我也用这个,请再看一次我的问题,我编辑了
    • .setData({'time' : FieldValue.serverTimestamp()});不导入,你能直接定义吗,我将如何在单独的变量中获取时间戳。 var timeStamp="你的代码";
    • 好的,检查更新的答案,运行它,让我知道它是否适合你
    【解决方案2】:

    你应该使用

    timestamp() {
        Firestore.instance.collection('timestamp').document('time').setData({
          "timestamp": FieldValue.serverTimestamp(),
        });
      }
    

    【讨论】:

    • 但我需要在单独的变量中。像这样 var timestamp=FieldValue.serverTimestamp();
    • 首先不要使用 var,因为它会降低可读性。如果您需要变量中的时间戳,您可以将其分配给外部设置数据对象,如下所示:timestamp = FieldValue.serverTimestamp();然后执行您想要执行的任何操作并分配给firestore的时间戳变量,例如“时间戳”:时​​间戳,
    猜你喜欢
    • 2019-09-18
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 2016-12-13
    • 2019-10-25
    相关资源
    最近更新 更多