connectivity_state_plus 0.1.6
connectivity_state_plus: ^0.1.6 copied to clipboard
Flutter plugin to detect and monitor real-time network connectivity status—including WiFi, cellular (mobile), and restricted networks—seamlessly across Android and iOS.
connectivity_state_plus #
This Flutter plugin to detect and monitor real-time network connectivity status—including WiFi, cellular (mobile), and restricted networks (e.g., captive portals)—seamlessly across Android and iOS.
Platform Support #
Android | iOS | MacOS | Web | Linux | Windows |
---|---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Requirements #
- Flutter >=3.19.0
- Dart >=3.3.0 <4.0.0
- iOS >=12.0
- MacOS >=10.14
- Android
compileSDK
34 - Java 17
- Android Gradle Plugin >=8.3.0
- Gradle wrapper >=8.4
Getting started #
published on pub.dev, run this Flutter command
flutter pub add connectivity_state_plus
Usage #
To verify actual internet reachability (beyond basic network type detection), configure your singleton Connectivity
instance with a base URL for active connectivity testing. When the device is connected to Wi-Fi/cellular but fails to reach the specified endpoint (e.g., due to captive portals or DNS issues), the state will fallback to ConnectivityState.restricted
instead of reporting a false positive.
String url = "https://pub.dev";
Connectivity().setAddressCheckOption(url);
Sample usage to listen for active connectivity state changes by subscribing to the stream.
import 'package:connectivity_state_plus/connectivity_state_plus.dart';
@override
initState() {
super.initState();
StreamSubscription<ConnectivityState> subscription = Connectivity().onConnectivityChanged.listen((ConnectivityState result) {
// Received changes in available connectivity state!
});
}
// Be sure to cancel subscription
@override
dispose() {
subscription.cancel();
super.dispose();
}
Sample usage to check currently available connection state:
import 'package:connectivity_state_plus/connectivity_state_plus.dart';
final ConnectivityState connectivityState = await (Connectivity().checkConnectivity());
Sample usage for detecting whether the current network connection uses a VPN:
[Note]: Only worked on iOS and macOS, otherwise would return ConnectivityState.other
or ConnectivityState.unknown
import 'package:connectivity_state_plus/connectivity_state_plus.dart';
final ConnectivityState connectivityState = await (Connectivity().checkVPNConnectivity());
Platform Support #
The following table shows which ConnectivityState
values are supported per platform.
Android | iOS | Web | MacOS | Windows | Linux | |
---|---|---|---|---|---|---|
restricted | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
wifi | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
mobile | ✅ | ✅ | ✅ | |||
vpn | ✅ | ✅ | ✅ | |||
other | ✅ | ✅ | ✅ | ✅ | ✅ |
none
and unknown
are supported on all platforms by default.
Android #
Connectivity changes are no longer communicated to Android apps in the background starting with Android O (8.0). You should always check for connectivity status when your app is resumed. The broadcast is only useful when your application is in the foreground.
iOS & MacOS #
On iOS simulators, the connectivity types stream might not update when Wi-Fi status changes. This is a known issue.
Starting with iOS 12 and MacOS 10.14, the implementation uses NWPathMonitor
to obtain the enabled connectivity types. We noticed that this observer can give multiple or unreliable results. For example, reporting connectivity "none" followed by connectivity "wifi" right after reconnecting.
We recommend to use the onConnectivityChanged
with this limitation in mind, as the method doesn't filter events, nor it ensures distinct values.
Web #
In order to retrieve information about the quality/speed of a browser's connection, the web implementation of the connectivity
plugin uses the browser's NetworkInformation Web API, which as of this writing (June 2020) is still "experimental", and not available in all browsers:
On desktop browsers, this API only returns a very broad set of connectivity statuses (One of 'slow-2g', '2g', '3g', or '4g'
), and may not provide a Stream of changes. Firefox still hasn't enabled this feature by default.