plus_plugins
plus_plugins copied to clipboard
[Bug]: onConnectivityChanged returning old state not the state that it was changed to.
Platform
iPhone 12
Plugin
connectivity_plus
Version
2.3.6 and 2.3.5
Flutter SDK
2.10.4
Steps to reproduce
I have wifi on my emulator turned on; when the connectivity is initialized it turnings a ConnectivityResult.none, and when I turn off wifi the listener returns a ConnectivityResult.wifi. This cycle persists regardless of the changes or order. So listening to the change is only returning the previous value not the new connectivity state.
Code Sample
import 'dart:async';
import 'dart:developer';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class ConnectivityNotifier extends StateNotifier<ConnectivityResult> {
ConnectivityNotifier() : super(ConnectivityResult.none) {
initConnectivity();
_connectivitySubscription =
_connectivity.onConnectivityChanged.listen((data) {
_updateConnectionStatus(data);
});
}
final Connectivity _connectivity = Connectivity();
late StreamSubscription<ConnectivityResult> _connectivitySubscription;
Future<void> initConnectivity() async {
late ConnectivityResult result;
print('ConnectivityNotifier initConnectivity');
try {
result = await _connectivity.checkConnectivity();
} on PlatformException catch (e) {
log('ConnectivityNotifier Couldn\'t check connectivity status', error: e);
return;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) {
return Future.value(null);
}
return _updateConnectionStatus(result);
}
Future<void> _updateConnectionStatus(ConnectivityResult result) async {
print('ConnectivityNotifier._updateConnectionStatus: $result');
state = result;
}
@override
void dispose() {
print('ConnectivityNotifier.dispose');
_connectivitySubscription.cancel();
super.dispose();
}
bool isConnected() {
return state != ConnectivityResult.none;
}
}
Logs
[ +1 ms] flutter: State variable after : ConnectivityResult.wifi
[+3767048 ms] flutter: State variable after : ConnectivityResult.wifi
[ ] flutter: connectivity from listener ConnectivityResult.wifi
[ ] flutter: ConnectivityNotifier._updateConnectionStatus: ConnectivityResult.wifi
[ ] flutter: State variable after : ConnectivityResult.wifi
[+12397 ms] flutter: State variable after : ConnectivityResult.wifi
[ +3 ms] flutter: connectivity from listener ConnectivityResult.none
[ ] flutter: ConnectivityNotifier._updateConnectionStatus: ConnectivityResult.none
[ +5 ms] flutter: State variable after : ConnectivityResult.none
[ +1 ms] flutter: State variable after : ConnectivityResult.none
[ ] flutter: connectivity from listener ConnectivityResult.none
[ +1 ms] flutter: ConnectivityNotifier._updateConnectionStatus: ConnectivityResult.none
[ +1 ms] flutter: State variable after : ConnectivityResult.none
Flutter Doctor
flutter doctor -v
[✓] Flutter (Channel unknown, 2.10.4, on macOS 12.4 21F79 darwin-arm, locale en-US)
• Flutter version 2.10.4 at /Users/aaronkennedy/Development/flutter
• Upstream repository unknown
• Framework revision c860cba910 (4 months ago), 2022-03-25 00:23:12 -0500
• Engine revision 57d3bac3dd
• Dart version 2.16.2
• DevTools version 2.9.2
[✗] Android toolchain - develop for Android devices
✗ Unable to locate Android SDK.
Install Android Studio from: https://developer.android.com/studio/index.html
On first launch it will assist you in installing the Android SDK components.
(or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
If the Android SDK has been installed to a custom location, please use
`flutter config --android-sdk` to update to that location.
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
[✓] VS Code (version 1.69.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.44.0
[✓] Connected device (2 available)
• iPhone 12 Pro (mobile) • DEDA0D01-91A3-47EB-A253-6C2F91DE0637 • ios • com.apple.CoreSimulator.SimRuntime.iOS-15-2 (simulator)
• Chrome (web) • chrome • web-javascript • Google Chrome 103.0.5060.114
[✓] HTTP Host Availability
• All required HTTP hosts are available
! Doctor found issues in 2 categories.
To clarify I also have this issue with the example app on emulator.
I also have this issue, any suggested solution/workaround for this?
My guess is that the currentConnectivityType
in this method is the old one when a new reachabilityChanged event is handled:
https://github.com/fluttercommunity/plus_plugins/blob/b206f0ae87972f55e1b3a81753c7512e6026c607/packages/connectivity_plus/connectivity_plus/macos/Classes/ReachabilityConnectivityProvider.swift#L57
and that's why it is giving the previous event all the time.
I don't know Swift enough to understand why it may be caching the previous value, but that would be my guess, if anyone wants to try to fix this:
https://github.com/fluttercommunity/plus_plugins/blob/b206f0ae87972f55e1b3a81753c7512e6026c607/packages/connectivity_plus/connectivity_plus/macos/Classes/ReachabilityConnectivityProvider.swift#L7
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days