Get current date in Dart

孤人 提交于 2021-02-17 06:27:05

问题


How can I get the current date independent of system date and time in Dart?

I have tried DateTime().now but it gives system date not the actual date (in case system date is altered).


回答1:


If you don't trust the system to give the correct time you need to ask somewhere else to get the time. This can be done in multiply ways depending on which accuracy you need. E.g. the easiest way would be to just call a HTTP server with an API to get the time from the server.

If accuracy are important you really need to use a NTP client to connect to multiple time sources (e.g. from ntppool.org). I can see there are a package for this here: https://pub.dev/packages/ntp

I would say that in most cases you should just trust the OS. If a user want to manipulate the time he/she should be able to do that. And if the time accuracy is important you don't want to trust the client but use a accuracy from you own server.



来源:https://stackoverflow.com/questions/57238903/get-current-date-in-dart

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