data_connection_checker 0.2.1 copy "data_connection_checker: ^0.2.1" to clipboard
data_connection_checker: ^0.2.1 copied to clipboard

outdated

Checks for an actual internet connection, by opening a socket connection to DNS Resolver addresses.

example/example.dart

import 'dart:async';

import 'package:data_connection_checker/data_connection_checker.dart';

// This of code will continue on running forever if not interrupted.
const Duration INTERVAL = Duration(seconds: 5);

main() async {
  // cspell:disable
  // check if we have the interwebz
  print("The statement 'this machine is connected to the Internet' is: ");
  print(await DataConnectionChecker().hasConnection);

  // We can also get an enum instead of a bool
  print(await DataConnectionChecker().connectionStatus);
  // prints either DataConnectionStatus.connected
  // or DataConnectionStatus.disconnected

  print('Results from the last check:');
  // this returns the results from the last call to either hasConnection or connectionStatus
  print(DataConnectionChecker().lastTryResults);

  // Check periodically, every 5 seconds, until the process is stopped
  _checkPeriodically();
}

void _checkPeriodically([Timer timer]) async {
  // we don't want other calls, until this one is done, so we cancel the timer
  // until this check completes
  print("Stopping the timer while we make a request.");
  timer?.cancel();

  print("Trying to ping: ${DataConnectionChecker().addresses}");

  bool result = await DataConnectionChecker().hasConnection;
  if (result) {
    print("We're part of the World. Hello, World! YAY!");
  } else {
    print('No cat pictures for the time being. So sad :(');
  }

  print("Starting the timer again...");
  Timer(INTERVAL, _checkPeriodically);
}
425
likes
0
pub points
94%
popularity

Publisher

unverified uploader

Checks for an actual internet connection, by opening a socket connection to DNS Resolver addresses.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on data_connection_checker