checkIsConnected method

bool checkIsConnected(
  1. List<ConnectivityResult> connectivityResult, {
  2. List<ConnectivityResult> allowedConnectivityOption = const [ConnectivityResult.mobile, ConnectivityResult.wifi, ConnectivityResult.ethernet, ConnectivityResult.vpn],
})

Checks if the current connectivity state represents a valid connection.

Validates the current connectivity results against a list of allowed connection types. Returns true if any of the active connections match the allowed types, false otherwise.

Parameters:

  • connectivityResult: List of currently active connectivity types
  • allowedConnectivityOption: List of connectivity types considered as valid connections. Defaults to mobile, WiFi, ethernet, and VPN.

Returns: true if connected via an allowed connection type, false otherwise.

Connection type priority (checked in order):

  1. Mobile network
  2. WiFi (on Android, WiFi takes precedence over mobile when both are active)
  3. Ethernet
  4. VPN (on iOS/macOS, VPN may be reported as 'other')
  5. Bluetooth
  6. Other network types
  7. None (no connection)

Example:

// Check with default allowed types (mobile, wifi, ethernet, vpn)
final results = await Connectivity().checkConnectivity();
bool isConnected = RtBaseHelper.instance.checkIsConnected(results);

// Check with custom allowed types (WiFi only)
bool isWifiConnected = RtBaseHelper.instance.checkIsConnected(
  results,
  allowedConnectivityOption: [ConnectivityResult.wifi],
);

Note:

  • On Android, when both mobile and WiFi are enabled, only WiFi is returned
  • On iOS/macOS, VPN connections may be reported as ConnectivityResult.other

Implementation

bool checkIsConnected(List<ConnectivityResult> connectivityResult,
    {List<ConnectivityResult> allowedConnectivityOption = const [
      ConnectivityResult.mobile,
      ConnectivityResult.wifi,
      ConnectivityResult.ethernet,
      ConnectivityResult.vpn
    ]}) {
  // Track connection state
  var isConnected = false;

  if (connectivityResult.contains(ConnectivityResult.mobile)) {
    // Mobile network available
    isConnected =
        allowedConnectivityOption.contains(ConnectivityResult.mobile)
            ? true
            : false;
  } else if (connectivityResult.contains(ConnectivityResult.wifi)) {
    // WiFi is available
    // Note for Android:
    // When both mobile and WiFi are turned on, system will return WiFi only
    isConnected = allowedConnectivityOption.contains(ConnectivityResult.wifi)
        ? true
        : false;
  } else if (connectivityResult.contains(ConnectivityResult.ethernet)) {
    // Ethernet connection available
    isConnected =
        allowedConnectivityOption.contains(ConnectivityResult.ethernet)
            ? true
            : false;
  } 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)
    isConnected = allowedConnectivityOption.contains(ConnectivityResult.vpn)
        ? true
        : false;
  } else if (connectivityResult.contains(ConnectivityResult.bluetooth)) {
    // Bluetooth connection available
    isConnected =
        allowedConnectivityOption.contains(ConnectivityResult.bluetooth)
            ? true
            : false;
  } else if (connectivityResult.contains(ConnectivityResult.other)) {
    // Connected to a network which is not in the above mentioned networks
    isConnected = allowedConnectivityOption.contains(ConnectivityResult.other)
        ? true
        : false;
  } else if (connectivityResult.contains(ConnectivityResult.none)) {
    // No available network types
    isConnected = false;
  }

  return isConnected;
}