data_connection_checker 0.3.2 data_connection_checker: ^0.3.2 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 'package:data_connection_checker/data_connection_checker.dart';
main() async {
// Simple check to see if we have internet
print("The statement 'this machine is connected to the Internet' is: ");
print(await DataConnectionChecker().hasConnection);
// returns a bool
// We can also get an enum instead of a bool
print("Current status: ${await DataConnectionChecker().connectionStatus}");
// prints either DataConnectionStatus.connected
// or DataConnectionStatus.disconnected
// This returns the last results from the last call
// to either hasConnection or connectionStatus
print("Last results: ${DataConnectionChecker().lastTryResults}");
// actively listen for status updates
var listener = DataConnectionChecker().onStatusChange.listen((status) {
switch (status) {
case DataConnectionStatus.connected:
print('Data connection is available.');
break;
case DataConnectionStatus.disconnected:
print('You are disconnected from the internet.');
break;
}
});
// close listener after 30 seconds, so the program doesn't run forever
await Future.delayed(Duration(seconds: 30));
await listener.cancel();
}