Viewing version 0.7.0. Open the latest package page.
connectivity_plus
0.7.0Flutter plugin for discovering the state of the network (WiFi & mobile/cellular) connectivity on Android and iOS.
Readme
connectivity_plus
Flutter Community: connectivity_plus
This plugin allows Flutter apps to discover network connectivity and configure themselves accordingly. It can distinguish between cellular vs WiFi connection. This plugin works for iOS and Android.
Note that on Android, this does not guarantee connection to Internet. For instance, the app might have wifi access but it might be a VPN or a hotel WiFi with no access.
Usage
Sample usage to check current status:
import 'package:connectivity_plus/connectivity.dart';
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
// I am connected to a mobile network.
} else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a wifi network.
}
Note that you should not be using the current network status for deciding whether you can reliably make a network connection. Always guard your app code against timeouts and errors that might come from the network layer.
You can also listen for network state changes by subscribing to the stream exposed by connectivity plugin:
import 'package:connectivity_plus/connectivity.dart';
@override
initState() {
super.initState();
subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
// Got a new connectivity status!
})
}
// Be sure to cancel subscription after you are done
@override
dispose() {
super.dispose();
subscription.cancel();
}
Note that connectivity changes are no longer communicated to Android apps in the background starting with Android O. You should always check for connectivity status when your app is resumed. The broadcast is only useful when your application is in the foreground.
You can get wi-fi related information using:
import 'package:connectivity_plus/connectivity.dart';
var wifiBSSID = await (Connectivity().getWifiBSSID());
var wifiIP = await (Connectivity().getWifiIP());network
var wifiName = await (Connectivity().getWifiName());wifi network
iOS 12
To use .getWifiBSSID() and .getWifiName() on iOS >= 12, the Access WiFi information capability in XCode must be enabled. Otherwise, both methods will return null.
iOS 13
The methods .getWifiBSSID() and .getWifiName() utilize the CNCopyCurrentNetworkInfo function on iOS.
As of iOS 13, Apple announced that these APIs will no longer return valid information. An app linked against iOS 12 or earlier receives pseudo-values such as:
-
SSID: "Wi-Fi" or "WLAN" ("WLAN" will be returned for the China SKU).
-
BSSID: "00:00:00:00:00:00"
An app linked against iOS 13 or later receives null.
The CNCopyCurrentNetworkInfo will work for Apps that:
-
The app uses Core Location, and has the user’s authorization to use location information.
-
The app uses the NEHotspotConfiguration API to configure the current Wi-Fi network.
-
The app has active VPN configurations installed.
If your app falls into the last two categories, it will work as it is. If your app doesn't fall into the last two categories, and you still need to access the wifi information, you should request user's authorization to use location information.
There is a helper method provided in this plugin to request the location authorization: requestLocationServiceAuthorization.
To request location authorization, make sure to add the following keys to your Info.plist file, located in <project root>/ios/Runner/Info.plist:
NSLocationAlwaysAndWhenInUseUsageDescription- describe why the app needs access to the user’s location information all the time (foreground and background). This is called Privacy - Location Always and When In Use Usage Description in the visual editor.NSLocationWhenInUseUsageDescription- describe why the app needs access to the user’s location information when the app is running in the foreground. This is called Privacy - Location When In Use Usage Description in the visual editor.
Limitations on the web platform
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:
Data on support for the netinfo feature across the major browsers from caniuse.com
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.
Fallback to navigator.onLine
For those browsers where the NetworkInformation Web API is not available, the plugin falls back to the NavigatorOnLine Web API, which is more broadly supported:
Data on support for the online-status feature across the major browsers from caniuse.com
The NavigatorOnLine API is provided by dart:html, and only supports a boolean connectivity status (either online or offline), with no network speed information. In those cases the plugin will return either wifi (when the browser is online) or none (when it's not).
Other than the approximate "downlink" speed, where available, and due to security and privacy concerns, no Web browser will provide any specific information about the actual network your users' device is connected to, like the SSID on a Wi-Fi, or the MAC address of their device.
Getting Started
For help getting started with Flutter, view our online documentation.
For help on editing plugin code, view the documentation.
Changelog
0.7.0
- Add Linux support (
connectivity_plus_linux).
0.6.0
- Fix issue #56: Rename Android and iOS classes and Method Channel to avoid collision with the original connectivity package.
0.5.0
- Transfer to plus-plugins monorepo
0.4.9+2
- Update package:e2e to use package:integration_test
0.4.9+1
- Update package:e2e reference to use the local version in the flutter/plugins repository.
0.4.9
- Add support for
web(by endorsingconnectivity_for_web0.3.0)
0.4.8+6
- Update lower bound of dart dependency to 2.1.0.
0.4.8+5
- Declare API stability and compatibility with
1.0.0(more details at: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0).
0.4.8+4
- Bump the minimum Flutter version to 1.12.13+hotfix.5.
- Clean up various Android workarounds no longer needed after framework v1.12.
- Complete v2 embedding support.
- Fix CocoaPods podspec lint warnings.
0.4.8+3
- Replace deprecated
getFlutterEnginecall on Android.
0.4.8+2
- Remove hard coded ios workspace setting of the example app.
0.4.8+1
- Make the pedantic dev_dependency explicit.
0.4.8
- Adds macOS as an endorsed platform.
0.4.7
- Migrate the plugin to use the ConnectivityPlatform.instance defined in the connectivity_plus_platform_interface package.
0.4.6+2
- Migrate deprecated BinaryMessages to ServicesBinding.instance.defaultBinaryMessenger.
- Bump Flutter SDK to 1.12.13+hotfix.5 or greater (current stable).
0.4.6+1
- Remove the deprecated
author:field from pubspec.yaml - Migrate the plugin to the pubspec platforms manifest.
- Require Flutter SDK 1.10.0 or greater.
0.4.6
- Add macOS support.
0.4.5+8
- Update documentation to explain when connectivity updates are received on Android.
0.4.5+7
- Fix unawaited futures in the example app and tests.
0.4.5+6
- Fix singleton Reachability problem on iOS.
0.4.5+5
- Add an analyzer check for the public documentation.
0.4.5+4
- Stability and Maintainability: update documentations.
0.4.5+3
- Remove AndroidX warnings.
0.4.5+2
- Include lifecycle dependency as a compileOnly one on Android to resolve potential version conflicts with other transitive libraries.
0.4.5+1
- Android: Use android.arch.lifecycle instead of androidx.lifecycle:lifecycle in
build.gradleto support apps that has not been migrated to AndroidX.
0.4.5
- Support the v2 Android embedder.
0.4.4+1
- Update and migrate iOS example project.
- Define clang module for iOS.
0.4.4
- Add
requestLocationServiceAuthorizationto request location authorization on iOS. - Add
getLocationServiceAuthorizationto get location authorization status on iOS. - Update README: add more information on iOS 13 updates with CNCopyCurrentNetworkInfo.
0.4.3+7
- Update README with the updated information about CNCopyCurrentNetworkInfo on iOS 13.
0.4.3+6
- [Android] Fix the invalid suppression check (it should be "deprecation" not "deprecated").
0.4.3+5
- [Android] Added API 29 support for
check(). - [Android] Suppress warnings for using deprecated APIs.
0.4.3+4
- [Android] Updated logic to retrieve network info.
0.4.3+3
- Support for TYPE_MOBILE_HIPRI on Android.
0.4.3+2
- Add missing template type parameter to
invokeMethodcalls.
0.4.3+1
- Fixes lint error by using
getApplicationContext()when accessing the Wifi Service.
0.4.3
- Add getWifiBSSID to obtain current wifi network's BSSID.
0.4.2+2
- Add integration test.
0.4.2+1
- Bump the minimum Flutter version to 1.2.0.
- Add template type parameter to
invokeMethodcalls.
0.4.2
- Adding getWifiIP() to obtain current wifi network's IP.
0.4.1
- Add unit tests.
0.4.0+2
- Log a more detailed warning at build time about the previous AndroidX migration.
0.4.0+1
- Updated
Connectivityto a singleton.
0.4.0
- Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.
0.3.2
- Adding getWifiName() to obtain current wifi network's SSID.
0.3.1
- Updated Gradle tooling to match Android Studio 3.1.2.
0.3.0
- Breaking change. Set SDK constraints to match the Flutter beta release.
0.2.1
- Fixed warnings from the Dart 2.0 analyzer.
- Simplified and upgraded Android project template to Android SDK 27.
- Updated package description.
0.2.0
- Breaking change. Upgraded to Gradle 4.1 and Android Studio Gradle plugin 3.0.1. Older Flutter projects need to upgrade their Gradle setup as well in order to use this version of the plugin. Instructions can be found here.
0.1.1
- Add FLT prefix to iOS types.
0.1.0
- Breaking API change: Have a Connectivity class instead of a top level function
- Introduce ability to listen for network state changes
0.0.1
- Initial release
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'connectivity_plus:0.7.0'pubspec.yaml
name: connectivity_plus
description: Flutter plugin for discovering the state of the network (WiFi & mobile/cellular) connectivity on Android and iOS.
version: 0.7.0
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
flutter:
plugin:
platforms:
android:
package: dev.fluttercommunity.plus.connectivity
pluginClass: ConnectivityPlugin
ios:
pluginClass: FLTConnectivityPlusPlugin
linux:
default_package: connectivity_plus_linux
macos:
default_package: connectivity_plus_macos
web:
default_package: connectivity_plus_web
dependencies:
flutter:
sdk: flutter
meta: ^1.0.5
connectivity_plus_platform_interface: ^0.3.0
connectivity_plus_linux: ^0.1.0
connectivity_plus_macos: ^0.3.0
connectivity_plus_web: ^0.5.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter_driver:
sdk: flutter
test: any
integration_test: ^0.9.1
mockito: ^4.1.1
plugin_platform_interface: ^1.0.0
pedantic: ^1.8.0
environment:
sdk: ">=2.1.0 <3.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | Cached · Upstream SHA-256 matched | Download | f353f957…ffbeeb |
| 0.5.0 | Cached · Upstream SHA-256 matched | Download | 400133e1…a4e841 |
| 0.6.0 | Cached · Upstream SHA-256 matched | Download | d95c068e…40b324 |
| 0.7.0 | Cached · Upstream SHA-256 matched | Download | c90fcf79…0d9575 |
| 0.7.1 | Cached · Upstream SHA-256 matched | Download | fd9f07b0…f79b5a |
| 0.7.2 | Cached · Upstream SHA-256 matched | Download | 7fb9587d…65bd71 |
| 0.8.0 | Cached · Upstream SHA-256 matched | Download | 647e9385…313705 |
| 0.8.1 | Cached · Upstream SHA-256 matched | Download | 8d565f7d…c9de03 |
| 0.9.0-nullsafety.0 | Cached · Upstream SHA-256 matched | Download | 9c50b950…802ba7 |
| 1.0.0 | Cached · Upstream SHA-256 matched | Download | 9bbf5d1a…394a13 |
| 1.0.1 | Cached · Upstream SHA-256 matched | Download | 54657e64…d5d25a |
| 1.0.2 | Cached · Upstream SHA-256 matched | Download | 1cdaf133…13c704 |
| 1.0.3 | Cached · Upstream SHA-256 matched | Download | c2738604…7ab813 |
| 1.0.4 | Cached · Upstream SHA-256 matched | Download | 7195b2b9…535aec |
| 1.0.5 | Cached · Upstream SHA-256 matched | Download | 19f93458…193355 |
| 1.0.6 | Cached · Upstream SHA-256 matched | Download | 9399efdf…39db45 |
| 1.0.7 | Cached · Upstream SHA-256 matched | Download | 3a4653a6…4b81e6 |
| 1.0.8 | Cached · Upstream SHA-256 matched | Download | a3d9a966…830238 |
| 1.1.0 | Cached · Upstream SHA-256 matched | Download | 1249fd8d…5d0cc5 |
| 1.2.0 | Cached · Upstream SHA-256 matched | Download | fcc92da7…c291bb |
| 1.3.0 | Cached · Upstream SHA-256 matched | Download | 7c56bb4d…ba20a4 |
| 1.4.0 | Cached · Upstream SHA-256 matched | Download | 77e0b376…b620cc |
| 2.0.0 | Cached · Upstream SHA-256 matched | Download | 81ab0366…35b6aa |
| 2.0.1 | Cached · Upstream SHA-256 matched | Download | c4028b19…13179d |
| 2.0.2 | Cached · Upstream SHA-256 matched | Download | 81fb9668…b599d3 |
| 2.0.3 | Cached · Upstream SHA-256 matched | Download | b5311d90…26d2d3 |
| 2.1.0 | Cached · Upstream SHA-256 matched | Download | 63b6c0cf…47908a |
| 2.2.0 | Cached · Upstream SHA-256 matched | Download | 78b23443…be93d1 |
| 2.2.1 | Cached · Upstream SHA-256 matched | Download | adf46fa1…60328c |
| 2.2.2 | Cached · Upstream SHA-256 matched | Download | 663e5fac…efd46f |
| 2.3.0 | Cached · Upstream SHA-256 matched | Download | 08c3225c…ef6a25 |
| 2.3.1 | Cached · Upstream SHA-256 matched | Download | 11b3f4da…bb7cdb |
| 2.3.2 | Cached · Upstream SHA-256 matched | Download | b3a377a2…7f5519 |
| 2.3.3 | Cached · Upstream SHA-256 matched | Download | 56dfb78c…d03fea |
| 2.3.4 | Cached · Upstream SHA-256 matched | Download | 3ea468d8…175a3b |
| 2.3.5 | Cached · Upstream SHA-256 matched | Download | 60a5c13b…f0a197 |
| 2.3.6 | Cached · Upstream SHA-256 matched | Download | e8eba38c…5c359d |
| 2.3.6+1 | Cached · Upstream SHA-256 matched | Download | e18bbd02…838d9d |
| 2.3.7 | Cached · Upstream SHA-256 matched | Download | c5eb92e2…7edb6d |
| 2.3.8 | Cached · Upstream SHA-256 matched | Download | 979d3b01…bd6908 |
| 2.3.9 | Cached · Upstream SHA-256 matched | Download | 3f8fe4e5…7e528e |
| 3.0.0 | Cached · Upstream SHA-256 matched | Download | 3461a36f…d87c0c |
| 3.0.1 | Cached · Upstream SHA-256 matched | Download | be54bbf8…302a3e |
| 3.0.2 | Cached · Upstream SHA-256 matched | Download | 745ebccc…013a22 |
| 3.0.3 | Cached · Upstream SHA-256 matched | Download | 8875e8ed…e48e96 |
| 3.0.4 | Cached · Upstream SHA-256 matched | Download | d73575bb…abc822 |
| 3.0.5 | Cached · Upstream SHA-256 matched | Download | 5c7ad2d9…e33e47 |
| 3.0.6 | Cached · Upstream SHA-256 matched | Download | b74247fa…95c991 |
| 4.0.0 | Cached · Upstream SHA-256 matched | Download | 45262924…005991 |
| 4.0.1 | Cached · Upstream SHA-256 matched | Download | 8599ae9e…913f34 |
| 4.0.2 | Cached · Upstream SHA-256 matched | Download | 77a180d6…95c48b |
| 5.0.0 | Cached · Upstream SHA-256 matched | Download | 94d51c6f…fcf70e |
| 5.0.1 | Cached · Upstream SHA-256 matched | Download | b502a681…46f55f |
| 5.0.2 | Cached · Upstream SHA-256 matched | Download | 224a7705…d437f0 |
| 6.0.0 | Cached · Upstream SHA-256 matched | Download | d3316397…7e9d01 |
| 6.0.1 | Cached · Upstream SHA-256 matched | Download | e9feae83…dad3c9 |
| 6.0.2 | Cached · Upstream SHA-256 matched | Download | ebe15d94…79fc57 |
| 6.0.3 | Cached · Upstream SHA-256 matched | Download | db7a4e14…4365b8 |
| 6.0.4 | Cached · Upstream SHA-256 matched | Download | 3e7d1d9d…8734f6 |
| 6.0.5 | Cached · Upstream SHA-256 matched | Download | 2056db52…7177d0 |
| 6.1.0 | Cached · Upstream SHA-256 matched | Download | 87684963…a3e4c3 |
| 6.1.1 | Cached · Upstream SHA-256 matched | Download | e0817759…f9c27d |
| 6.1.2 | Cached · Upstream SHA-256 matched | Download | 8a68739d…938412 |
| 6.1.3 | Cached · Upstream SHA-256 matched | Download | 04bf81bb…19ec27 |
| 6.1.4 | Cached · Upstream SHA-256 matched | Download | 051849e2…cc1f99 |
| 6.1.5 | Cached · Upstream SHA-256 matched | Download | b5e72753…ab29ec |
| 7.0.0 | Cached · Upstream SHA-256 matched | Download | 33bae12a…c3973c |
| 7.1.0 | Cached · Upstream SHA-256 matched | Download | b8fe5297…166c79 |
| 7.1.1 | Cached · Upstream SHA-256 matched | Download | 62ffa266…5ba057 |
| 7.2.0 | Cached · Upstream SHA-256 matched | Download | cad0e811…313160 |
| 7.3.0 | Cached · Upstream SHA-256 matched | Download | 25cebb79…de33cb |
| 7.3.1 | Cached · Upstream SHA-256 matched | Download | 762c99f8…a54bd0 |
| 7.3.2 | Cached · Upstream SHA-256 matched | Download | d7749fd3…36f741 |
Dependencies
connectivity_plus_linux "^0.1.0"
connectivity_plus_macos "^0.3.0"
connectivity_plus_platform_interface "^0.3.0"
connectivity_plus_web "^0.5.0"
flutter {
"sdk": "flutter"
}
meta "^1.0.5"