internet_connection_checker 1.0.0+1 internet_connection_checker: ^1.0.0+1 copied to clipboard
A pure Dart library that checks for internet by opening a socket to a list of specified addresses, each with individual port and timeout. Defaults are provided for convenience.
import 'dart:async';
import 'package:internet_connection_checker/internet_connection_checker.dart';
Future<void> main() async {
// Check internet connection with singleton (no custom values allowed)
await execute(InternetConnectionChecker());
// Create customized instance which can be registered via dependency injection
final InternetConnectionChecker customInstance =
InternetConnectionChecker.createInstance(
checkTimeout: const Duration(seconds: 1),
checkInterval: const Duration(seconds: 1),
);
// Check internet connection with created instance
await execute(customInstance);
}
Future<void> execute(
InternetConnectionChecker internetConnectionChecker,
) async {
// Simple check to see if we have Internet
// ignore: avoid_print
print('''The statement 'this machine is connected to the Internet' is: ''');
final bool isConnected = await InternetConnectionChecker().hasConnection;
// ignore: avoid_print
print(
isConnected.toString(),
);
// returns a bool
// We can also get an enum instead of a bool
// ignore: avoid_print
print(
'Current status: ${await InternetConnectionChecker().connectionStatus}',
);
// Prints either InternetConnectionStatus.connected
// or InternetConnectionStatus.disconnected
// actively listen for status updates
final StreamSubscription<InternetConnectionStatus> listener =
InternetConnectionChecker().onStatusChange.listen(
(InternetConnectionStatus status) {
switch (status) {
case InternetConnectionStatus.connected:
// ignore: avoid_print
print('Data connection is available.');
break;
case InternetConnectionStatus.disconnected:
// ignore: avoid_print
print('You are disconnected from the internet.');
break;
}
},
);
// close listener after 30 seconds, so the program doesn't run forever
await Future<void>.delayed(const Duration(seconds: 30));
await listener.cancel();
}