ntp 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 93

pub package

ntp #

Plugin that allows you to get precise time from Network Time Protocol (NTP). Whole NTP protocol is implemented in dart.

This is useful for time-based events since DateTime.now() returns time of the device. Users sometimes change their internal clock and using DateTime.now() can give really wrong result. You can just get clock offset [NTP.getNtpTime] and apply it manually to DateTime.now() object when needed (just add offset as milliseconds duration), or you can get already formatted [DateTime] object from [NTP.now].

By default lookup address for NTP is: pool.ntp.org

For example on how to use look in github library repository example/ folder.

How it works #

Using int offset from getNtpTime()

  • default localTime is DateTime.now()
  • default lookUpAddress is 'pool.ntp.org'
  • default port is 123
  DateTime startDate = new DateTime().now().toLocal();
  int offset = await NTP.getNtpTime(localTime: startDate);
  print('NTP DateTime offset align: ${startDate.add(new Duration(milliseconds: offset))}');

Using DateTime from now

  DateTime startDate = await NTP.now();
  print('NTP DateTime: ${startDate}');

NTP Functions #

  Future<int> getNtpTime({
    String lookUpAddress: 'pool.ntp.org',
    int port: 123,
    DateTime localTime,
  Future<DateTime> now();

[1.0.1] - 22.10.2018.

  • Updated description and code cleanup

[1.0.1] - 22.07.2018.

  • Added example
  • Updated README.md

[1.0.0] - 18.07.2018.

  • Methods in NTP are now static.

  • Added now() for returning current DateTime object with calculated NTP offset

  • getNtpTime was renamed to getNtpOffset (since it is returning int offset in milliseconds)

[0.0.1] - 16.04.2018.

  • Get NTP clock offset


import 'package:flutter/material.dart';
import 'package:ntp/ntp.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  _MyAppState createState() => new _MyAppState();

class _MyAppState extends State<MyApp> {
  DateTime _currentTime;
  DateTime _ntpTime;
  int _ntpOffset;

  void initState() {


  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Example of NTP library'),
        body: Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _showData('Current time:', '$_currentTime'),
              _showData('NTP offset:', '$_ntpOffset ms'),
              _showData('NTP time:', '$_ntpTime'),
        floatingActionButton: new FloatingActionButton(
          tooltip: 'Update time',
          child: const Icon(Icons.timer),
          onPressed: _updateTime,

  Widget _showData(String title, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          new Text(title,
              style: Theme.of(context)
                  .copyWith(fontWeight: FontWeight.w500, fontSize: 16.0)),
          new Text(value,
              style: Theme.of(context)
                  .copyWith(fontWeight: FontWeight.w300, fontSize: 16.0)),

  void _updateTime() async {
    _currentTime = DateTime.now();

    NTP.getNtpOffset().then((int value) {
      setState(() {
        _ntpOffset = value;
        _ntpTime = _currentTime.add(Duration(milliseconds: _ntpOffset));

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  ntp: ^1.0.2

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:ntp/ntp.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Nov 5, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

Package is getting outdated. (-3.56 points)

The package was last published 54 weeks ago.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies