Flutter 颤振聊天应用程序保存来自设备时间的消息时间戳

Flutter 颤振聊天应用程序保存来自设备时间的消息时间戳,flutter,dart,timestamp,Flutter,Dart,Timestamp,在用户互相传递的消息中创建相同的时间戳时,我遇到了一个问题 这是一个发送消息的功能: void onSendMessage(String content, int type) { if (content.trim() != '') { textEditingController.clear(); var documentReference = Firestore.instance .collection('messages')

在用户互相传递的消息中创建相同的时间戳时,我遇到了一个问题

这是一个发送消息的功能:

  void onSendMessage(String content, int type) {
    if (content.trim() != '') {
      textEditingController.clear();

      var documentReference = Firestore.instance
          .collection('messages')
          .document(groupChatId)
          .collection(groupChatId)
          .document(DateTime.now().millisecondsSinceEpoch.toString());

      Firestore.instance.runTransaction((transaction) async {
        await transaction.set(
          documentReference,
          {
            'idFrom': id,
            'idTo': peerId,
            'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
            'content': content,
            'type': type
          },
        );
      });

      cacheSet(groupChatId, content, peerNickname, peerAvatar);
      listScrollController.animateTo(0.0,
          duration: Duration(milliseconds: 300), curve: Curves.easeOut);
    } else {
      Fluttertoast.showToast(msg: 'Nothing to send');
    }
  }
但是,此DateTime.now.millissecondssinceepoch.toString会根据用户的设备创建时间戳。这意味着如果userA在10:00向userB发送一条消息hello,但如果userB回复world,并且他的设备时间是9:50,则该消息将显示在userA的消息之前,该消息为false

大概是这样的:

09:50 userB - "world"
10:00 userA - "hello"

是否存在与设备时间无关的通用时间格式?是否可以在不使用外部API的情况下进行设置?

在将代码更改为FieldValue.serverTimestamp之前,我遇到了这个问题

将DateTime.now.millissecondssinceepoch.toString更改为FieldValue.serverTimestamp

所以试试这个:

 {
        'idFrom': id,
        'idTo': peerId,
        'timestamp': FieldValue.serverTimestamp(),
        'content': content,
        'type': type
  },
检查这些:


希望这有帮助

但当我尝试创建一个文档时,我得到了'FieldValue'的实例,您好,您是否检查了我提供的链接?因为它不是字符串,所以文档使用字符串作为参数,将其用作文档名称。如果我可以问的话,你为什么要使用事务处理?检查。