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 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 the singleton instance of InternetConnection.
factory
-
InternetConnection.createInstance({Duration? checkInterval, List<
InternetCheckOption> ? customCheckOptions, bool useDefaultOptions = true, bool enableStrictCheck = false, ConnectivityCheckCallback? customConnectivityCheck, Stream? triggerStream}) - Creates an instance of InternetConnection.
Properties
- checkInterval → Duration
-
Returns the current duration between connection checks.
no setter
- customConnectivityCheck → ConnectivityCheckCallback?
-
Function to check reachability of a single network endpoint.
final
- enableStrictCheck → bool
-
If
true, all checks must be successful to consider the internet as connected.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
- triggerStream → Stream?
-
An optional stream that triggers an immediate internet connection check
whenever it emits an event.
final
Methods
-
dispose(
) → Future< void> - Disposes of the resources used by this instance.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
setIntervalAndResetTimer(
Duration duration) → void -
Updates the interval between connection checks to the given
durationand resets the connection checking timer. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited