Last updated:
0 purchases
internet connection checker
🌍 Internet Connection Checker #
A library designed for seamless internet connectivity checks.
This library enables you to verify your internet connection.
Table of contents #
🌍 Internet Connection Checker
Table of contents
Description
Demo
Quick start
Purpose
Getting Started
Singleton example
Internet Connection Availability changes
Creating a Custom Instance
Custom Response Code Logic
Features and bugs
Description #
Checks for an internet (data) connection, by checking accessible Uri's.
The defaults of the plugin should be sufficient to reliably determine if
the device is currently connected to the global network, e.i. has access to the Internet.
Platform Support #
Android
iOS
MacOS
Web
Linux
Windows
✅
✅
✅
✅
✅
✅
Demo #
Quick start #
Add the following dependencies to your pubspec.yaml:
dependencies:
internet_connection_checker: ^2.0.0
copied to clipboard
Android Configuration #
On Android, for correct working in release mode, you must add INTERNET & ACCESS_NETWORK_STATE
permissions to AndroidManifest.xml, follow the next lines:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Permissions for internet_connection_checker -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
...
copied to clipboard
Purpose #
The reason this package exists is that connectivity_plus package cannot reliably determine if a data connection is actually available. More info on its page here: https://pub.dev/packages/connectivity_plus.
Getting Started #
The InternetConnectionChecker can be used as a singleton or can be instantiated with a custom instance with your curated AddressCheckOption's.
Best usage with Flutter Bloc/Cubit #
The InternetConnectionChecker package is particularly useful in scenarios where you need to handle changes in network connectivity dynamically. For instance, you can use it to refresh the page or trigger specific actions when the device goes offline and then reconnects to the internet. This is useful for applications where real-time updates or data synchronization is critical.
See example folder for one specific example which illustrates how to retry page refresh when internet connection is available. The example files paths are listed below:
example/lib/blocs/fetch_todos_cubit.dart
example/lib/pages/auto_refresh_when_network_is_available_page.dart
copied to clipboard
Singleton Basic Usage example #
import 'package:internet_connection_checker/internet_connection_checker.dart';
void main() async {
bool isConnected = await InternetConnectionChecker().hasConnection;
if (isConnected) {
print('Device is connected to the internet');
} else {
print('Device is not connected to the internet');
}
}
copied to clipboard
Listen to Stream for Internet Connection availability changes: #
import 'package:internet_connection_checker/internet_connection_checker.dart';
void main() {
final connectionChecker = InternetConnectionChecker();
final subscription = connectionChecker.onStatusChange.listen(
(InternetConnectionStatus status) {
if (status == InternetConnectionStatus.connected) {
print('Connected to the internet');
} else {
print('Disconnected from the internet');
}
},
);
// Remember to cancel the subscription when it's no longer needed
subscription.cancel();
}
copied to clipboard
Note: Remember to dispose of any listeners,
when they're not needed to prevent memory leaks,
e.g. in a StatefulWidget's dispose() method:
...
@override
void dispose() {
listener.cancel();
super.dispose();
}
...
copied to clipboard
Creating a Custom Instance #
To create a custom instance of InternetConnectionChecker with specific check options:
import 'package:internet_connection_checker/internet_connection_checker.dart';
void main() async {
final customChecker = InternetConnectionChecker.createInstance(
customCheckOptions: [
AddressCheckOption(uri: Uri.parse('https://1.1.1.1')),
AddressCheckOption(
uri: Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
),
],
useDefaultOptions: false,
);
bool isConnected = await customChecker.hasConnection;
print('Custom instance connected: $isConnected');
}
copied to clipboard
Custom Response Code Logic #
import 'package:internet_connection_checker/internet_connection_checker.dart';
void main() async {
final customChecker = InternetConnectionChecker.createInstance(
customCheckOptions: [
AddressCheckOption(
uri: Uri.parse('https://img.shields.io/pub/'),
responseStatusFn: (response) {
return response.statusCode == 404;
},
),
],
useDefaultOptions: false,
);
bool isConnected = await customChecker.hasConnection;
print('Custom response logic connected: $isConnected');
}
copied to clipboard
See example folder for more examples.
Features and bugs #
Please file feature requests and bugs at the issue tracker.
Credits #
NOTE: This package is a continuation of data_connection_checker which currently is not continued. *
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.