InternetConnection class
A utility class for checking internet connectivity status.
This class provides functionality to monitor and verify internet connectivity by checking reachability to various Uris. It relies on the connectivity_plus package for listening to connectivity changes and the http package for making network requests.
Usage
Checking for internet connectivity
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
bool result = await InternetConnection().hasInternetAccess;
Listening for internet connectivity changes
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
final listener = InternetConnection().onStatusChange.listen(
(InternetStatus status) {
switch (status) {
case InternetStatus.connected:
// The internet is now connected
break;
case InternetStatus.disconnected:
// The internet is now disconnected
break;
}
},
);
Don't forget to cancel the subscription when it is no longer needed. This will prevent memory leaks and free up resources.
listener.cancel();
Constructors
- InternetConnection()
-
Returns an instance of InternetConnection.
factory
-
InternetConnection.createInstance({Duration checkInterval = const Duration(seconds: 5), List<
InternetCheckOption> ? customCheckOptions, bool useDefaultOptions = true}) - Creates an instance of InternetConnection.
Properties
- checkInterval → Duration
-
The duration between consecutive status checks.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
hasInternetAccess
→ Future<
bool> -
Checks if there is internet access by verifying connectivity to the
specified Uris.
no setter
-
internetStatus
→ Future<
InternetStatus> -
Returns the current internet connection status.
no setter
- lastTryResults → InternetStatus?
-
The result of the last attempt to check the internet status.
no setter
-
onStatusChange
→ Stream<
InternetStatus> -
Stream that emits internet connection status changes.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited