Connectivity package's onConnectionChanged() being called multiple times
Asked Answered
E

0

8

I have setup a basic check using Connectivity's Readme on checking internet connectivity.

@override
  void initState() {
    super.initState();
    subscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult result) {
      // Got a new connectivity status!
      print("Check: $result");
    });

  }

  @override
  void dispose() {
    print("Disposing Connection ...");
    super.dispose();
    subscription.cancel();
  }

I haven't made any significant changes to the code itself. However, I when I checked the logs, It seems like the app is calling the function onConnectivityChanged() twice, everytime a change in connection occurs.

Here are the logs:

Restarted application in 1,611ms.
I/flutter (17481): Check: ConnectivityResult.none
I/flutter (17481): Check: ConnectivityResult.mobile
I/flutter (17481): Check: ConnectivityResult.mobile
I/flutter (17481): Check: ConnectivityResult.none
I/flutter (17481): Check: ConnectivityResult.none
I/flutter (17481): Check: ConnectivityResult.wifi
I/flutter (17481): Check: ConnectivityResult.wifi

The first result is ConnectivityResult.none and it appears only once. Because the app checks the connectivity only once when the current widget loads. However, after the first (correct) result, all the results are obtained twice, even though they should come out only once.

This has led to some complications in my app. Everytime I see a connection change, I show a dialog box to the user with the following options:

  • You are offline

  • You are on mobile network

But since the check somehow runs twice on connection changes, I have been seeing two dialog boxes. Not only that, since when I pop the dialog box out of the context, the app loads once again and does the check on initState() causing the dialog box to show again. It will continue unless the phone is on WiFi, where there are no checks.

Ermina answered 21/11, 2019 at 8:24 Comment(10)
I just save the previous state and if the new event is for an unchanged state, ignore it.Proulx
But as you see the code has been calling initState() twice. Is this normal behaviour? Should I add a bug report to flutter's github?Ermina
Connectivity state is app wide, so you should subscribe to it right up in your material app state, or highest provider or inherited widget.Proulx
will do. Thanks!Ermina
Did you found the solution? I am facing this too. My function is getting called multiple times.Deflate
What problem are you facing exactly? Is your initState() is being called multiple times? or are you talking about other functions?Ermina
I have written the subscription in init. As the network gets connected I called function. But the function inside is called multiple times.Deflate
subscription = Connectivity() .onConnectivityChanged .listen((ConnectivityResult result) async { _connectionStatus = result.toString(); if (result == ConnectivityResult.wifi || result == ConnectivityResult.mobile) { var res = await _sqLiteHelper.getVisitorDataList(); if (res.length >= 1) { _createVisitorEntry(_clientSiteId, _clientDeviceId, _authKey, true); } } else { checkInternet = false; } }); The function _createVisitorEntry is being called multiple times.Deflate
I am also facing the same issue. Any solution for this ? It is creating more troubles when app comes from offline to onlineDoenitz
As @RichardHeap has mentioned. try subscribing to connectivity on the launcher route, like main.lib. This might be how flutter is developed by design and IMHO we can't change it, otherwise, something else might break. Although initState(){} is called only once. I can't wrap my head around it either, but you should be better off subscribing to connectivity in the first screen itself. Good luck and Happy Coding :)Ermina

© 2022 - 2024 — McMap. All rights reserved.