A Flutter package to check your internet connection with subsecond response times, even on mobile networks!
This library 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.
- β Check internet connectivity status
- β Listen to internet connectivity changes
- β Customizable endpoints and success criteria
- β Customizable connectivity check logic
Features | Android | iOS | macOS | Linux | Windows | Web |
---|---|---|---|---|---|---|
Check Connectivity | β | β | β | β | β | β |
Listen to Changes | β | β | β | β | β | β |
Add the following permission to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.INTERNET" />
Add the following to your macOS .entitlements
files:
<key>com.apple.security.network.client</key>
<true/>
For more information, see the Flutter Networking Documentation.
The simplest way to check if you have internet access:
final bool isConnected = await InternetConnection().hasInternetAccess;
if (isConnected) {
print('Connected!');
} else {
print('No internet connection.');
}
The InternetConnection
class exposes a stream of InternetStatus
updates,
allowing you to react to changes in connectivity:
final subscription = InternetConnection().onStatusChange.listen(
(InternetStatus status) {
if (status == InternetStatus.connected) {
// Internet is connected
} else {
// Internet is disconnected
}
},
);
Note
Don't forget to cancel the subscription when it is no longer needed. This will prevent memory leaks and free up resources:
@override
void dispose() {
subscription.cancel();
super.dispose();
}
You can specify your own endpoints to check for connectivity:
final connection = InternetConnection.createInstance(
customCheckOptions: [
InternetCheckOption(uri: Uri.parse('https://example.com')),
],
);
final isConnected = await connection.hasInternetAccess;
Important
- Make sure the endpoints have no caching enabled.
- On
web
platform, make sure the endpoints are not CORS blocked.
You can define what counts as a successful response:
final connection = InternetConnection.createInstance(
customCheckOptions: [
InternetCheckOption(
uri: Uri.parse('https://example.com'),
responseStatusFn: (response) {
return response.statusCode >= 69 && response.statusCode < 169;
},
),
InternetCheckOption(
uri: Uri.parse('https://example2.com'),
responseStatusFn: (response) => response.statusCode == 420,
),
],
);
final isConnected = await connection.hasInternetAccess;
For advanced use cases, you can completely customize how connectivity checks are performed by providing your own connectivity checker:
final connection = InternetConnection.createInstance(
customConnectivityCheck: (option) async {
// Example: Use the Dio http client
try {
final dio = Dio();
final response = await dio.head(
option.uri,
options: Options(headers: option.headers, receiveTimeout: option.timeout, validateStatus: (_) => true),
);
return InternetCheckResult(
option: option,
isSuccess: response.statusCode == 200,
);
} catch (_) {
return InternetCheckResult(option: option, isSuccess: false);
}
},
);
This customization gives you full control over the connectivity detection process, allowing you to:
- Implement platform-specific network detection
- Use alternate connectivity checking strategies
- Implement custom fallback mechanisms
- Add detailed logging or metrics for connectivity checks
- Integrate with other network monitoring tools
For situation where you want to pause any network requests when the app goes into the background and resume them when the app comes back into the foreground (see issue #27):
class MyWidget extends StatefulWidget {
const MyWidget({super.key});
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
late final StreamSubscription<InternetStatus> _subscription;
late final AppLifecycleListener _listener;
@override
void initState() {
super.initState();
_subscription = InternetConnection().onStatusChange.listen((status) {
// Handle internet status changes
});
_listener = AppLifecycleListener(
onResume: _subscription.resume,
onHide: _subscription.pause,
onPause: _subscription.pause,
);
}
@override
void dispose() {
_subscription.cancel();
_listener.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// Build your widget
}
}
The enableStrictCheck
option can be used to require that all checked URIs
must respond successfully for the internet to be considered available. By
default, only one successful response is required.
final connection = InternetConnection.createInstance(
enableStrictCheck: true,
customCheckOptions: [
InternetCheckOption(uri: Uri.parse('https://example.com')),
InternetCheckOption(uri: Uri.parse('https://example2.com')),
],
);
Caution
Use enableStrictCheck
only with custom-defined URIs, not with the default
ones.
Using it with the default URIs may lead to unreliable results or service outages, as all default endpoints must be up and reachable for a positive result.
The following endpoints are checked by default:
URI | Description |
---|---|
https://one.one.one.one | Response time is less than 100ms , CORS enabled, no-cache |
https://icanhazip.com | Response time is less than 100ms , CORS enabled, no-cache |
https://jsonplaceholder.typicode.com/todos/1 | Response time is less than 100ms , CORS enabled, no-cache |
https://pokeapi.co/api/v2/ability/?limit=1 | Response time is less than 100ms , CORS enabled, no-cache |
The following URIs are tested and work well with the package:
URI | Description |
---|---|
https://ipapi.co/ip | CORS enabled, no-cache |
https://api.adviceslip.com/advice | CORS enabled, no-cache |
https://api.bitbucket.org/2.0/repositories | CORS enabled, no-cache |
https://api.thecatapi.com/v1/images/search | CORS enabled, no-cache |
https://randomuser.me/api/?inc=gender | CORS enabled, no-cache |
https://dog.ceo/api/breed/husky/list | CORS enabled, no-cache |
https://lenta.ru | Russia supported, CORS enabled, no-cache |
https://www.gazeta.ru | Russia supported, CORS enabled, no-cache |
If you liked the package, then please give it a Like ππΌ and Star β
This package is a cloned and modified version of the internet_connection_checker package, which itself was based on data_connection_checker (now unmaintained).
The main goal of this package is to provide a more reliable and faster solution for checking internet connectivity in Flutter applications.