簡體   English   中英

如何正確保存 DateTime 到 Cloud Firestore

[英]How to save DateTime to Cloud Firestore correct

在我的應用程序中,我創建了一個聊天。 當某人與另一個用戶聊天時,他們必須查看某人何時發送了他的消息。 在我的腳本中,我嘗試使用DateTime 但是,如果有人在最后 10 分鍾內發送消息,例如,他們都有相同的時間。 class MessageTile用於消息,而addMessage方法是我在有人單擊發送圖標(如時間)時存儲內容的地方。 有誰知道我如何獲得正確的Date來節省我的時間,以便當有人在 12:00 發送他的消息並且有人在 13:15 發送他的消息時,這會顯示在聊天中?

我的聊天屏幕截圖

class MessageTile extends StatelessWidget {
  final String message;
  final bool sendByMe;
  final String time;

  MessageTile({@required this.message, @required this.sendByMe, @required this.time});


  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: sendByMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          padding: EdgeInsets.only(
              top: 3,
              bottom: 3,
              left: sendByMe ? 0 : 24,
              right: sendByMe ? 24 : 0),
          alignment: sendByMe ? Alignment.centerRight : Alignment.centerLeft,
          child: Container(
            margin: sendByMe
                ? EdgeInsets.only(left: 30)
                : EdgeInsets.only(right: 30),
            padding: EdgeInsets.only(
                top: 17, bottom: 17, left: 20, right: 20),
            decoration: BoxDecoration(
                borderRadius: sendByMe ? BorderRadius.only(
                  topLeft: Radius.circular(9),
                  topRight: Radius.circular(9),
                  bottomLeft: Radius.circular(9),
                ) :
                BorderRadius.only(
                    topLeft: Radius.circular(9),
                    topRight: Radius.circular(9),
                    bottomRight: Radius.circular(9)),
                color: sendByMe ? Colors.blue : Colors.white
            ),
            child: Column(
              children: [
                Text(message,
                  textAlign: TextAlign.start,
                  style: TextStyle(
                    color: Colors.black,
                    fontWeight: FontWeight.bold,
                    fontFamily: 'Orbitron',
                    fontSize: 12.5,),),
                Text( DateFormat("hh:mm").format(DateTime.parse(time.toString())) ,style:
                TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontFamily: 'Orbitron', fontSize: 7.0,) ,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}

像這樣??

addMessage() {
    if (messageEditingController.text.isNotEmpty) {
      Map<String, dynamic> chatMessageMap = {
        "sendBy": Constants.myName,
        "message": messageEditingController.text,
        'time': DateFormat("hh:mm").format(DateTime.parse(DateTime.now().toLocal().toString())) ,
      };

      DatabaseService().addMessage(widget.chatRoomId, chatMessageMap);

      setState(() {
        messageEditingController.text = "";
      });
    }
  }

那個

 addMessage() {
    if (messageEditingController.text.isNotEmpty) {
      Map<String, dynamic> chatMessageMap = {
        "sendBy": Constants.myName,
        "message": messageEditingController.text,
        'time': DateFormat("hh:mm").format(DateTime.parse(DateTime.now().toLocal().toString())) ,
      };

      FirebaseFirestore.instance.collection('myChatCollection').doc().set({
        'timeStamp': ServerValue.timestamp
      });

      DatabaseService().addMessage(widget.chatRoomId, chatMessageMap);

      setState(() {
        messageEditingController.text = "";
      });
    }
  }

Firestore中存儲Date and Time的最佳方式是使用服務器的日期和時間。 不鼓勵使用DateTime.now()來存儲時間,因為用戶可以將手機的日期和時間更改為他們想要的任何內容。 使用 Firestore 服務器的時間戳存儲它:

FirebaseFirestore.instance.collection('myChatCollection').doc().set({
  'timeStamp': ServerValue.timestamp
});

請注意,時間存儲在 UTC+0

將其更改為:

 addMessage() {
    if (messageEditingController.text.isNotEmpty) {
      Map<String, dynamic> chatMessageMap = {
        "sendBy": Constants.myName,
        "message": messageEditingController.text,
        'time': ServerValue.timestamp,
      };

     FirebaseFirestore.instance.collection('myChatCollection').doc().set(chatMessageMap);
      DatabaseService().addMessage(widget.chatRoomId, chatMessageMap);

      setState(() {
        messageEditingController.text = "";
      });
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM