stacktrace2234
stacktrace2234

Reputation: 1500

Flutter connectivity: Works on Android, but on iOS simulator when I can open webpages in Safari I have internet but the app says there is no internet?

I use this package: https://pub.dev/packages/connectivity_plus

I have a finished application that is working on Android but when I am testing it on iOS it shows that there is no internet. I can use and open pages in Safari so there is definitely one. But the following code returns false in iOS:

class InternetConnectivity with ChangeNotifier {
  StreamSubscription<ConnectivityResult>? _subscription;
  bool haveInternet = false;

  void checkConnectivity() {
    if (_subscription == null) {
      _subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
        bool res = result == ConnectivityResult.mobile || result == ConnectivityResult.wifi;
        setHaveInternet = res;
      });
    }
  }

  set setHaveInternet(bool value) {
    if (haveInternet != value) {
      haveInternet = value;
      notifyListeners();
    }
  }
}

I don't get any errors so I don't really know where to look for the problem.

On the screen where it checks that internet connection starts with this:

bool _haveInternet = true;

then in initState() I set the value of it:

  @override
  void initState() {
    super.initState();
    
    InternetConnectivity ? _internetConnectivity = InternetConnectivity();

    setState(() {
      _haveInternet = _internetConnectivity!.haveInternet;
    });

After the initState() ran, the _haveInternet becomes false, so the connectivity_plus package returns false while normally it should be true.

Thanks in advance.

Upvotes: 1

Views: 3205

Answers (3)

Arthur Alves
Arthur Alves

Reputation: 21

You can add an empty onConnectivityChanged in your code, after this, the method Connectivity().checkConnectivity() will return a correct value.

like this:

Connectivity().onConnectivityChanged;

final ConnectivityResult connectivityResult =
    await (Connectivity().checkConnectivity());

Upvotes: 0

christophe chanteur
christophe chanteur

Reputation: 1

it works with this work around:

in your stream listenener (mine is called _updateConnectivityStatus), make this simple check:

Future<void> _updateConnectivityStatus(
    ConnectivityResult connectivityResult) async {

 bool isConnected = connectivityResult = ConnectivityResult.mobile || connectivityResult == ConnectivityResult.wifi || connectivityResult == ConnectivityResult.ethernet || connectivityResult == ConnectivityResult.vpn;

if (Platform.isIOS) {
isConnected = !isConnected;
}
//etc...
}
}

Another method is to use the following package, the internet_connection_checker_plus package, which also shows "connected" status when internet data network exists (meaning when actual data can be downloaded or uploaded via any network outlet, wifi, cellular, bluetooth...), contrary to connectivity_plus package which "connected" status doesn't mean the device is connected to the internet but rather only connected to the network router.This package can be used as a standalone, or with conjunction with the connectivity_plus package. Both of these work (whether that package alone or coupled with the connectivity_plus package) I for now chose to use it by itself, and removed the connectivity_plus package.

Upvotes: 0

Marius Lastauskas
Marius Lastauskas

Reputation: 71

The package has a bug. According to documentation it should only affect iOS simulator. https://github.com/fluttercommunity/plus_plugins/issues/479

From package comments:

  /// On iOS, the connectivity status might not update when WiFi
  /// status changes, this is a known issue that only affects simulators.
  /// For details see https://github.com/fluttercommunity/plus_plugins/issues/479.

Upvotes: 2

Related Questions