diff --git a/docs/connectivity_plus/overview.mdx b/docs/connectivity_plus/overview.mdx index a191055e3a..3210ebb6e9 100644 --- a/docs/connectivity_plus/overview.mdx +++ b/docs/connectivity_plus/overview.mdx @@ -6,14 +6,8 @@ hide_title: true ## Connectivity Plus Overview -Connectivity plus is Flutter plugin that allows your apps to get information about -the network connectivity of the device, whether it's running on cellular or WiFi, -and if it has an internet connection or not. - -:::note -Connectivity plus is going to replace the [original Connectivity plugin](https://pub.dev/packages/connectivity), -if you have been using it, it's better to switch to this one. -::: +Connectivity Plus is a Flutter plugin that allows your apps to get information about +currently active network types on a device, whether it's running on cellular, WiFi or other connection types. ### Get started diff --git a/docs/connectivity_plus/usage.mdx b/docs/connectivity_plus/usage.mdx index 5f5d5f1a18..3bebe4c61d 100644 --- a/docs/connectivity_plus/usage.mdx +++ b/docs/connectivity_plus/usage.mdx @@ -19,25 +19,29 @@ Using Connectivity API, you can find out which type of connection the device is ```dart import 'package:connectivity_plus/connectivity_plus.dart'; -final 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. -} else if (connectivityResult == ConnectivityResult.ethernet) { - // I am connected to a ethernet network. -} else if (connectivityResult == ConnectivityResult.vpn) { - // I am connected to a vpn network. +final List connectivityResult = await (Connectivity().checkConnectivity()); + +// This condition is for demo purposes only to explain every connection type. +// Use conditions which work for your requirements. +if (connectivityResult.contains(ConnectivityResult.mobile)) { + // Mobile network available. +} else if (connectivityResult.contains(ConnectivityResult.wifi)) { + // Wi-fi is available. + // Note for Android: + // When both mobile and Wi-Fi are turned on system will return Wi-Fi only as active network type +} else if (connectivityResult.contains(ConnectivityResult.ethernet)) { + // Ethernet connection available. +} else if (connectivityResult.contains(ConnectivityResult.vpn)) { + // Vpn connection active. // Note for iOS and macOS: // There is no separate network interface type for [vpn]. // It returns [other] on any device (also simulator) -} else if (connectivityResult == ConnectivityResult.bluetooth) { - // I am connected to a bluetooth. -} else if (connectivityResult == ConnectivityResult.other) { - // I am connected to a network which is not in the above mentioned networks. -} else if (connectivityResult == ConnectivityResult.none) { - // I am not connected to any network. +} else if (connectivityResult.contains(ConnectivityResult.bluetooth)) { + // Bluetooth connection available. +} else if (connectivityResult.contains(ConnectivityResult.other)) { + // Connected to a network which is not in the above mentioned networks. +} else if (connectivityResult.contains(ConnectivityResult.none)) { + // No available network types } ``` @@ -58,12 +62,12 @@ user's connectivity. #### 1. Declare a StreamSubscription ``` dart -StreamSubscription _connectivitySubscription; +StreamSubscription> _connectivitySubscription; ``` #### 2. Listen to the Stream -Use `onConnectivityChanged` method that has a return type `Stream`, +Use `onConnectivityChanged` method that returns `Stream>`, to register a listener. :::note @@ -75,13 +79,13 @@ The broadcast is only useful when your application is in the **foreground**. ``` dart // Initialize a variable with [none] status to avoid nulls at startup -ConnectivityResult _connectivityResult = ConnectivityResult.none; +List _connectionStatus = [ConnectivityResult.none]; @override void initState() { super.initState(); _connectivitySubscription = - _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); + Connectivity().onConnectivityChanged.listen(_updateConnectionStatus); } // Make sure to cancel subscription after you are done @@ -95,9 +99,9 @@ dispose() { The listener will update the connectivity status. ``` dart -Future _updateConnectionStatus(ConnectivityResult result) async { +Future _updateConnectionStatus(List result) async { setState((){ - _connectivityResult = result; + _connectionStatus = result; }); } ``` diff --git a/packages/connectivity_plus/connectivity_plus/README.md b/packages/connectivity_plus/connectivity_plus/README.md index 0c4782b01c..e03f5a7bc6 100644 --- a/packages/connectivity_plus/connectivity_plus/README.md +++ b/packages/connectivity_plus/connectivity_plus/README.md @@ -10,8 +10,7 @@
build

-This plugin allows Flutter apps to discover network connectivity and configure -themselves accordingly. It can distinguish between cellular vs WiFi connection. +This plugin allows Flutter apps to discover network connectivity types that can be used. > **Note** > @@ -25,29 +24,34 @@ themselves accordingly. It can distinguish between cellular vs WiFi connection. ## Usage -Sample usage to check current status: +Sample usage to check currently available connection types: ```dart import 'package:connectivity_plus/connectivity_plus.dart'; -final 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. -} else if (connectivityResult == ConnectivityResult.ethernet) { - // I am connected to a ethernet network. -} else if (connectivityResult == ConnectivityResult.vpn) { - // I am connected to a vpn network. +final List connectivityResult = await (Connectivity().checkConnectivity()); + +// This condition is for demo purposes only to explain every connection type. +// Use conditions which work for your requirements. +if (connectivityResult.contains(ConnectivityResult.mobile)) { + // Mobile network available. +} else if (connectivityResult.contains(ConnectivityResult.wifi)) { + // Wi-fi is available. + // Note for Android: + // When both mobile and Wi-Fi are turned on system will return Wi-Fi only as active network type +} else if (connectivityResult.contains(ConnectivityResult.ethernet)) { + // Ethernet connection available. +} else if (connectivityResult.contains(ConnectivityResult.vpn)) { + // Vpn connection active. // Note for iOS and macOS: // There is no separate network interface type for [vpn]. // It returns [other] on any device (also simulator) -} else if (connectivityResult == ConnectivityResult.bluetooth) { - // I am connected to a bluetooth. -} else if (connectivityResult == ConnectivityResult.other) { - // I am connected to a network which is not in the above mentioned networks. -} else if (connectivityResult == ConnectivityResult.none) { - // I am not connected to any network. +} else if (connectivityResult.contains(ConnectivityResult.bluetooth)) { + // Bluetooth connection available. +} else if (connectivityResult.contains(ConnectivityResult.other)) { + // Connected to a network which is not in the above mentioned networks. +} else if (connectivityResult.contains(ConnectivityResult.none)) { + // No available network types } ``` @@ -55,8 +59,8 @@ if (connectivityResult == ConnectivityResult.mobile) { > > 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: +You can also listen for active connectivity types changes by subscribing to the stream +exposed by the plugin: ```dart import 'package:connectivity_plus/connectivity_plus.dart'; @@ -65,8 +69,8 @@ import 'package:connectivity_plus/connectivity_plus.dart'; initState() { super.initState(); - subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) { - // Got a new connectivity status! + StreamSubscription> subscription = Connectivity().onConnectivityChanged.listen((List result) { + // Received changes in available connectivity types! }); } @@ -80,7 +84,7 @@ dispose() { > **Note** > -> 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. +> 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. ## Limitations on the web platform