HomeiOS Developmentandroid - Flutter connectivity_plus not updating GetX observable on WiFi toggle

android – Flutter connectivity_plus not updating GetX observable on WiFi toggle


I am utilizing Flutter with connectivity_plus and GetX to handle web connectivity globally throughout my app.

The difficulty is:
Once I toggle WiFi on/off, the .isConnected observable in my ConnectivityController doesn’t replace. It at all times prints true even when WiFi is turned off.

// primary.dart

void primary() async {
      WidgetsFlutterBinding.ensureInitialized();
      // Initialize world controller
      Get.put(ConnectivityController());
      runApp(const MyApp());
    }

// connectivity_controller.dart

class ConnectivityController extends GetxController {
  RxBool isConnected = true.obs;
  RxBool isInitialized = false.obs;

  @override
  void onInit() {
    tremendous.onInit();
    _initConnectivity();
  }

  Future _initConnectivity() async {
    attempt {
      closing outcome = await Connectivity().checkConnectivity();
      isConnected.worth = outcome != ConnectivityResult.none;
      isInitialized.worth = true;

      Connectivity().onConnectivityChanged.pay attention((standing) {
        closing newValue = standing != ConnectivityResult.none;
        if (isConnected.worth != newValue) {
          isConnected.worth = newValue;
        }
      });
    } catch (e) {
      isConnected.worth = false;
      isInitialized.worth = true;
    }
  }
}

// Verify in Widget

GestureDetector(
  onTap: () {
    print('verify........');
    print(_connectivityController.isConnected.worth);
  },
  little one: Icon(CupertinoIcons.hand_draw),
),

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments