Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Issue with onConnectivityChanged listener on iOS: ConnectivityResult.none followed by ConnectivityResult.wifi even when connected to Wifi #3453

Open
8 tasks done
0minKoh opened this issue Feb 3, 2025 · 7 comments
Labels
bug Something isn't working triage

Comments

@0minKoh
Copy link

0minKoh commented Feb 3, 2025

Platform

iOS 18

Plugin

connectivity_plus

Version

6.1.1

Flutter SDK

3.27.3

Steps to reproduce

Steps to reproduce:

  1. Install the app on a real iPhone 13 Pro device via Ad hoc distribution.
  2. Run the app and perform a Hot Restart by pressing Cmd + R.
  3. Observe the connection status changes through the _connectivity.onConnectivityChanged listener.

Observed behavior:

Even when connected to Wifi, the connection status changes are observed in the following order:

flutter: Connectivity Status: [ConnectivityResult.none]
flutter: Connectivity Status: [ConnectivityResult.wifi]

This issue sometimes occurs when navigating between screens using Navigator.of(context).pushAndRemoveUntil().
A similar issue was reported in #479, but that issue was described as only occurring in the simulator.

Code Sample

_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);

//...

Future<void> _updateConnectionStatus(List<ConnectivityResult> result) async {
  print("Connectivity Status: $result");
  if (result.contains(ConnectivityResult.none)) {
    // ...
  }
}

Logs

flutter: Connectivity Status: [ConnectivityResult.none]
flutter: Connectivity Status: [ConnectivityResult.wifi]

Flutter Doctor

[✓] Flutter (Channel stable, 3.27.3, on macOS 15.2 24C101 darwin-arm64, locale ko-KR)
    • Flutter version 3.27.3 on channel stable at
      /Users/sohyunwoo/Desktop/developer/flutter/flutter-sdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c519ee916e (13일 전), 2025-01-21 10:32:23 -0800
    • Engine revision e672b006cb
    • Dart version 3.6.1
    • DevTools version 2.40.2

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/sohyunwoo/Library/Android/sdk/
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/sohyunwoo/Library/Android/sdk/
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)

[✓] VS Code (version 1.96.4)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.102.0

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 15.2 24C101
      darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 15.2 24C101
      darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome
      132.0.6834.160
    ! Error: Browsing on the local area network for 고영민의 iPhone. Ensure the device is unlocked and attached
      with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for 고영민의 iPhone. Ensure the device is unlocked and attached
      with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Checklist before submitting a bug

  • I searched issues in this repository and couldn't find such bug/problem
  • I Google'd a solution and I couldn't find it
  • I searched on StackOverflow for a solution and I couldn't find it
  • I read the README.md file of the plugin
  • I'm using the latest version of the plugin
  • All dependencies are up to date with flutter pub upgrade
  • I did a flutter clean
  • I tried running the example project
@0minKoh 0minKoh added bug Something isn't working triage labels Feb 3, 2025
@miquelbeltran
Copy link
Member

It isn't clear to me if you are running the app as installed from your device, or from the flutter run command, as you say you perform a hot-restart.

onConnectivityChanged reports events as they come from the operating system, we only apply a distinct, because we got complaints that the Stream would emit too many events.

So if you are performing a hot-restart, it could be that iOS's NWPathMonitor cannot obtain the status when the hot restart happens and so the stream emits a none temporarily.

@davidnunes19
Copy link

Hello,

I am experiencing the same issue, whether running the app from an isolated bundle or using flutter run.
The device in use is an iPhone 13 with iOS 18.

The first call to check the connection returns [ConnectivityResult.none],
The second call returns [ConnectivityResult.wifi]. These are independent calls.

When using the listener, I sequentially receive both states (none, wifi).

It appears that the two states are emitted in quick succession. Even though this might be an OS-level call, it could be due to native events being emitted incorrectly or an issue specific to iOS 18.

Are there any references or an estimated timeline for a resolution?

@miquelbeltran
Copy link
Member

estimated timeline for a resolution?

This is a community project run by volunteers, don't expect anyone from the team working on this. If anyone from the community finds out and submits a solution, then I am happy to review the PR.

@0minKoh
Copy link
Author

0minKoh commented Feb 4, 2025

@miquelbeltran

I understand, sorry for my mistake what I write on section of 'how to reproduce'.
However, I could to find same issue on other situation which different to 'hot restart case'
I should seek for more case of this issue and I add more appropriate way to reproduce.

thank you for your response.

@0minKoh
Copy link
Author

0minKoh commented Feb 4, 2025

@davidnunes19

Temporary Workaround for Incorrect Network Status Detection

I have a temporary workaround for handling connectivity issues in Flutter.

Initially, I used the onConnectivityChanged listener to detect network changes and navigate to a network error screen whenever ConnectivityResult.none appeared:

_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);

Future<void> _updateConnectionStatus(List<ConnectivityResult> result) async {
  print("Connectivity Status: $result");
  if (result.contains(ConnectivityResult.none)) {
    // Example: Navigate to network error page
  }
}

However, this approach is not suitable for certain cases. Even when the network is actually connected, it might incorrectly display a network error to users.

Improved Approach: Double-Checking Connectivity

A better approach is to use checkConnectivity() when result.contains(ConnectivityResult.none) occurs. This ensures that we verify the actual network state before navigating to an error screen. The updated code looks like this:

if (result.contains(ConnectivityResult.none)) {
  List<ConnectivityResult> connectivityState = await _connectivity.checkConnectivity();
  if (!connectivityState.contains(ConnectivityResult.none)) {
    return;
  }
  // Proceed with network error handling
}

By implementing this, we prevent users from being redirected to a network error page due to a temporary ConnectivityResult.none state.

Next Steps

I hope this solution helps you and other developers facing similar issues. Additionally, I will investigate the root cause and, if possible, submit a PR to address this problem at the source.

Thanks!

@miquelbeltran
Copy link
Member

That makes sense to me.

From a design point of view, I wouldn't rely on connectivity_plus to show an error message to the user, as it can show a none status temporarily as seen by some reports. That would be fine if you want to show a connection indicator somewhere that it is not disturbing the UX.

Remember as well that having connectivity doesn't mean you are actually connected to the internet, but rather just that the OS is connected to some network. There are other plugins that track actual internet connection and those may be a better choice for this use case.

@davidnunes19
Copy link

Thanks for your workaround @0minKoh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
Development

No branches or pull requests

3 participants