在我的应用中,我创建了一个聊天。当某人与另一个用户聊天时,他们必须查看某人何时发送了他的消息。在我的脚本中,我尝试使用DateTime
。但是,例如,如果有人在最近10分钟内发送邮件,则他们都具有相同的时间。类MessageTile
用于消息,而addMessage
方法是当某人单击“发送图标”(如时间)时我在其中存储内容的地方。有谁知道我如何获得正确的Date
来节省时间,以便当某人在12:00发送其消息而某人在13:15发送其消息时该消息正在聊天中显示?
A Screenshot from my Chatscreen
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 = "";
});
}
}
最佳答案
在Date and Time
中存储Firestore
的最佳方法是使用服务器的日期和时间。不建议使用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 = "";
});
}
}
关于firebase - 如何正确将DateTime保存到Cloud Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63987687/