Dart/Flutter Not converting local date time to UTC correctly

梦想的初衷 提交于 2021-02-11 15:14:30

问题


I am building a flutter/firebase chat app and storing timestamps of messages in UTC. I am using this line of code to convert local time to UTC. 'time': DateTime.now().toUtc().toString(), When I tested this with two users one is in Virginia, US, and the other one in Sri Lanka. The timestamp of the user in Sri Lanka is converted correctly to UTC, but for user in Virginia time is correct but the date as of yesterday.

Sri Lanka Time: 2020-06-21 20:50:48

UTC converted: 2020-06-21 15:20:48.027446Z

Virginia Time: 2020-06-21 11:24:38

UTC converted: 2020-06-20 15:24:38.594194Z

My whole app malfunctioned because of this and order of messages got weird. How to solve this issue?


回答1:


You don't need to store the timestamp by yourself. Firestore offers timestamp. See https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/FieldValue/serverTimestamp.html



来源:https://stackoverflow.com/questions/62502699/dart-flutter-not-converting-local-date-time-to-utc-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!