I created a small app where you can add comments to posts, and this is the code to add the comment to firestore
final DateTime timestamp = DateTime.now();
addComment() async{
commentsRef.doc(postId).collection('comments').add({
'username': current.uniqueName,
'comment': commentController.text,
'timestamp': timestamp,
'avatarUrl': current.profilePictureURL,
'userId': current.userID
});}
But, if I post a comment and after a couple of seconds I post another one, both the posts have the same timestamp. What can it be related to? How can I solve this problem?
CodePudding user response:
The correct usage is:
addComment() async{
commentsRef.doc(postId).collection('comments').add({
'username': current.uniqueName,
'comment': commentController.text,
'timestamp': DateTime.now(),
'avatarUrl': current.profilePictureURL,
'userId': current.userID
});}
You store the timestamp in the final
variable, and never update it. That's why you get the same value in the database.