如何在firestore中保存时间戳

SOS*_*deo 3 timestamp dart flutter google-cloud-firestore

我将下面显示的地图保存到 Firestore,但我还想保存一个时间戳,其中应该包含用户使用日期和时间选择器选择的时间和日期。日期已采用“dd.MM.yyyy”格式,时间类似于“6:58 PM”。日期是变量date,时间是变量time。如何保存带有值time和 的时间戳date

Map<String, dynamic> data = {
      'name': userName,
      'userUID': userUIDglobal,
      'time: '//here I want to safe the timestamp
    };
    Firestore.instance
        .collection('seller')
        .document(documentID)
        .collection('test')
        .add(data);
  }
Run Code Online (Sandbox Code Playgroud)

nst*_*sic 9

如果您想使用服务器生成的值,这样您就可以不依赖设备时间,请使用以下命令:

    Map<String, dynamic> data = {
      'name': userName,
      'userUID': userUIDglobal,
      'time': FieldValue.serverTimestamp(),
    };
    Firestore.instance
        .collection('seller')
        .document(documentID)
        .collection('test')
        .add(data);
Run Code Online (Sandbox Code Playgroud)

但请注意,如果您有一个活动的集合快照订阅,当您将此文档写入集合时,您将获得一个具有null值的快照time(因为乐观提交),然后当您从服务器获取数据时,您将获得一个快照。获取实际的毫秒值。

null编辑:您可以通过将此写入作为事务的一部分来防止乐观提交发出时间戳值。