connectivity_plus_linux_portal 0.0.2
connectivity_plus_linux_portal: ^0.0.2 copied to clipboard
A Linux implementation of connectivity_plus that uses xdg_desktop_portal
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].
}
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"
Manual testing #
flutter run -d linux --dart-define=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).
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 #
- @sgehrman for sharing the approach
- xdg_desktop_portal by Canonical