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 Uri
s. 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 for internet connectivity changes
Platform | Check Connectivity | Listen for Changes |
---|---|---|
Android | ✅ | ✅ |
iOS | ✅ | ✅ |
macOS | ✅ | ✅ |
Linux | ✅ | ✅ |
Windows | ✅ | ✅ |
Web | ✅ | ✅ |
Add the following permissions to your AndroidManifest.xml
file:
<uses-permission android:name="android.permission.INTERNET" />
Add the following permissions to your macOS .entitlements
files:
<key>com.apple.security.network.client</key>
<true/>
For more information, see the Flutter Networking Documentation.
Add the internet_connection_checker_plus
package to your pubspec.yaml
file:
dependencies:
internet_connection_checker_plus: ^2.6.0
Import the internet_connection_checker_plus
package into your Dart file:
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
The simplest way to check for internet connectivity is to use the
InternetConnection
class:
bool result = await InternetConnection().hasInternetAccess;
The InternetConnection
class also provides a stream of InternetStatus
that
can be used to listen for changes in internet connectivity:
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();
The InternetConnection
class can be configured to check custom Uri
s for
internet connectivity:
final connection = InternetConnection.createInstance(
customCheckOptions: [
InternetCheckOption(uri: Uri.parse('https://example.com')),
],
);
Important
Make sure the custom Uri
s have no caching enabled. Otherwise, the results
may be inaccurate.
Important
On web
platform, make sure the custom Uri
s are not CORS blocked.
Otherwise, the results may be inaccurate.
The InternetConnection
class can be configured to check custom Uri
s for
internet connectivity using custom success criteria:
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) {
return response.statusCode >= 420 && response.statusCode < 1412;
},
),
],
);
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 InternetConnection
class uses the following Uri
s 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://reqres.in/api/users/1 |
Response time is less than 100ms , CORS enabled, no-cache |
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://api.coindesk.com/v1/bpi/currentprice.json |
CORS enabled, no-cache |
https://lenta.ru |
Russia supported, CORS enabled, no-cache |
https://www.gazeta.ru |
Russia supported, CORS enabled, no-cache |
This package is a cloned and modified version of the internet_connection_checker package which is a cloned and modified version of the data_connection_checker package which is no longer maintained.
The aim of this package is to support the web
platform which is currently not
supported by the internet_connection_checker package.