问题
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