fix: handle connectivity_plus PlatformException on Windows

This commit is contained in:
edde746
2026-03-03 20:13:17 +01:00
parent 19c36ca340
commit 174c78bf67
2 changed files with 36 additions and 13 deletions
+29 -12
View File
@@ -28,10 +28,15 @@ class OfflineModeProvider extends ChangeNotifier {
/// Updates network and server connection flags
Future<void> _updateConnectionFlags() async {
final connectivityResult = await Connectivity()
.checkConnectivity()
.timeout(const Duration(seconds: 3), onTimeout: () => [ConnectivityResult.other]);
_hasNetworkConnection = !connectivityResult.contains(ConnectivityResult.none);
try {
final connectivityResult = await Connectivity()
.checkConnectivity()
.timeout(const Duration(seconds: 3), onTimeout: () => [ConnectivityResult.other]);
_hasNetworkConnection = !connectivityResult.contains(ConnectivityResult.none);
} catch (e) {
// connectivity_plus can throw PlatformException on Windows (NetworkManager::StartListen)
_hasNetworkConnection = true;
}
_hasServerConnection = _serverManager.onlineServerIds.isNotEmpty;
}
@@ -43,15 +48,27 @@ class OfflineModeProvider extends ChangeNotifier {
// Check initial connectivity
await _updateConnectionFlags();
// Monitor connectivity changes
_connectivitySubscription = Connectivity().onConnectivityChanged.listen((results) {
final wasOffline = isOffline;
_hasNetworkConnection = !results.contains(ConnectivityResult.none);
// Monitor connectivity changes — wrapped in try-catch because
// connectivity_plus can throw synchronously on Windows (NetworkManager::StartListen)
try {
_connectivitySubscription = Connectivity().onConnectivityChanged.listen(
(results) {
final wasOffline = isOffline;
_hasNetworkConnection = !results.contains(ConnectivityResult.none);
if (wasOffline != isOffline) {
notifyListeners();
}
});
if (wasOffline != isOffline) {
notifyListeners();
}
},
onError: (e) {
// Assume network available on stream error
_hasNetworkConnection = true;
},
);
} catch (e) {
// Assume network available if stream activation fails
_hasNetworkConnection = true;
}
// Monitor server status from MultiServerManager
_serverStatusSubscription = _serverManager.statusStream.listen((statusMap) {
+7 -1
View File
@@ -264,7 +264,13 @@ class DownloadManagerService {
final settings = await SettingsService.getInstance();
if (!settings.getDownloadOnWifiOnly()) return false;
final connectivity = await Connectivity().checkConnectivity();
final List<ConnectivityResult> connectivity;
try {
connectivity = await Connectivity().checkConnectivity();
} catch (e) {
// connectivity_plus can throw PlatformException on Windows — don't block
return false;
}
// Block if on cellular and NOT on WiFi (allow if both are available)
return connectivity.contains(ConnectivityResult.mobile) &&
!connectivity.contains(ConnectivityResult.wifi) &&