NtpClient class

A web-based implementation of an NTP (Network Time Protocol) client.

This implementation retrieves the current server time via an HTTP API rather than using a raw UDP socket (which is not available in web environments).

By default, it queries the Postman Echo /time/now endpoint, which returns the current GMT time as a string (e.g. "Thu, 17 Jul 2025 12:34:56 GMT"). It also natively supports Cloudflare Trace (ts=...), ISO-8601 strings, and standard HTTP Date headers without requiring package:intl.

Inheritance

Constructors

NtpClient({String server = 'pool.ntp.org', int port = 123, int timeout = 5, String? apiUrl, DateTime parseResponse(Response)?, bool isUtc = false})
Creates a new NtpClient instance with optional configuration parameters.
const

Properties

apiUrl → String?
The URL of the API endpoint to query for time (Web only).
finalinherited
hashCode → int
The hash code for this object.
no setterinherited
isUtc → bool
Whether to return the time in UTC (true) or local time (false).
finalinherited
parseResponse → DateTime Function(Response)?
A function to parse the response from the API endpoint (Web only).
finalinherited
port → int
The UDP port used to communicate with the NTP server.
finalinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
server → String
The hostname or IP address of the NTP server to query.
finalinherited
timeout → int
The timeout in seconds for the time retrieval operation.
finalinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
now() → Future<DateTime>
Fetches the current server time from the configured HTTP API.
override
query({int samples = 1}) → Future<NtpResponse>
Queries the configured HTTP time endpoint and returns a full NtpResponse.
override
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

parseWebDateString(String body, {String? dateHeader}) → DateTime
Parses an HTTP-date (RFC 1123), Cloudflare trace (ts=...), or ISO-8601 string into a UTC DateTime without external dependencies.