Linux Portal implementation for connectivity_plus

A Linux implementation of connectivity_plus that uses XDG Desktop Portal (org.freedesktop.portal.NetworkMonitor) for sandbox compliance (e.g., Flathub/Flatpak).

Getting Started

Call ConnectivityPlusLinuxPortalPlugin.registerWith() either for all Linux desktops or only under certain conditions (i.e., Flatpak detection):

import 'dart:io';

import 'package:connectivity_plus_linux_portal/connectivity_plus_linux_portal.dart';

void main() {
  if (Platform.isLinux) {
    final isFlatpak =
        Platform.environment.containsKey('FLATPAK_ID') ||
        Platform.environment['container'] == 'flatpak';
    final backend = Platform.environment['CONNECTIVITY_BACKEND'];
    final usePortal = isFlatpak || backend == 'portal';

    if (usePortal) {
      ConnectivityPlusLinuxPortalPlugin.registerWith();
    }
  }
}

The connectivity_plus plugin will now use the portal when running the Flatpak version of the app:

import 'package:connectivity_plus/connectivity_plus.dart';

Future<void> main() async {
  final result = await Connectivity().checkConnectivity();
  print(result); // Typically [ConnectivityResult.none] or [ConnectivityResult.other].
}

Manual testing

CONNECTIVITY_BACKEND=portal flutter run -d linux

Flatpak manifest

Pass the environment variable in the Flatpak manifest (optional fallback in case FLATPAK_ID env variable was not found):

finish-args:
  - --env=CONNECTIVITY_BACKEND=portal

Motivation

Address upstream issue.

The default connectivity_plus Linux implementation uses nm, which uses org.freedesktop.NetworkManager, which causes:

Unhandled Exception: org.freedesktop.DBus.Error.ServiceUnknown: org.freedesktop.DBus.Error.ServiceUnknown

While you you can add (last resort):

finish-args:
  - --talk-name=org.freedesktop.NetworkManager

This approach is often discouraged when submitting an app to Flathub (example comment).

This package uses xdg_desktop_portal. Both xdg_desktop_portal and nm uses the dbus package. All three packages are maintained by Canonical.

Limitations

This package uses org.freedesktop.portal.NetworkMonitor instead of org.freedesktop.NetworkManager.

As a result, fewer ConnectivityResult types are supported (by design for sandbox/privacy reasons):

  • No network: ConnectivityResult.none
  • Connected to a network: ConnectivityResult.other (still unknown)
  • Captive portal: Assumes ConnectivityResult.wifi
  • Network is metered: Assumes ConnectivityResult.mobile

Tip

This limitation is not relevant when only the general connectivity state is required (connected vs disconnected), independent of the underlying transport (e.g., Wifi, Ethernet). In many cases, this is not an issue (e.g., serverpod_flutter).

You can use the portal implementation only for the Flatpak version (e.g., via environment variable or argument) and keep the default implementation (as shown in this section).

Acknowledgement

Libraries

connectivity_plus_linux_portal
A Linux implementation of connectivity_plus that uses XDG Desktop Portal (org.freedesktop.portal.NetworkMonitor) for sandbox compliance (e.g., Flathub/Flatpak).