Kronos - NTP plugin for Flutter
Unlike the device clock, the time reported by Kronos is unaffected when the local time is changed while your app is running. Instead, Kronos stores accurate time along with a delta between the NTP time and the system uptime. Since uptime increases monotonically, Kronos isn't affected by device time changes. Accessing KronosClock.getCurrentTimeMs() will return the local time based on the last known accurate time + delta since last sync.
Since it relies on system uptime, Kronos detects and requires a new sync after each reboot.
This plugin is based on Kronos in iOS and Kronos-Android in android for the whole SNTP protocol implementation.
Why Kronos?
Kronos provides a single source of truth for time in your application, independent of device settings. Accurate time is crucial for many real-world applications:
- Financial transactions: Ensuring precise timestamps for trading, payments, and transaction records
- Security & Authentication: Time-sensitive tokens, session management, and security protocols that rely on accurate time
- Real-time collaboration: Synchronizing events across multiple users and devices
- Analytics & Logging: Accurate event tracking and debugging with reliable timestamps
- Scheduling & Reminders: Ensuring timely notifications regardless of device clock manipulation
- Gaming: Fair gameplay with synchronized game states and preventing time-based cheating
- IoT & Sensors: Coordinating time-sensitive data collection across multiple devices
- Offline-first apps: Maintain accurate time even when offline after initial sync, perfect for apps with local-first architecture
Kronos protects your app from users who manually change their device time, either accidentally or intentionally, ensuring your app always has access to accurate time. Once synced, Kronos continues to provide accurate time even without internet connection by leveraging system uptime, making it the reliable source of truth for time in your application.
Installation
Just add flutter_kronos as a dependency in your pubspec.yaml file.
Android
Add the following permission in your Android Manifest file:
<uses-permission android:name="android.permission.INTERNET"/>
Usage
Sync clock using a pool of NTP servers
Calling FlutterKronos.sync will fire a bunch of NTP requests to up to 5 of the
servers on the given NTP pool. As soon as
we get the first response, the given closure is called but the FlutterKronos
will keep trying to get a more accurate response.
FlutterKronos.sync();
Get an NTP time
int? FlutterKronos.getCurrentTimeMs(); //return time from the fallback clock if Kronos has not yet been synced
int? FlutterKronos.getCurrentNtpTimeMs(); //return null if Kronos has not yet been synced
DateTime? FlutterKronos.getDateTime(); //return null if Kronos has not yet been synced
DateTime? FlutterKronos.getNtpDateTime(); //return null if Kronos has not yet been synced
Support
If you have any questions or issues, please contact me at hautv.fami@gmail.com
Donate
If you find this plugin helpful, consider supporting the development: