Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuously check internet disconnection on Flutter app

I'm creating a mobile application on Flutter and I'm calling REST API's created on Node.js in order to connect and query my oracle DB all the time.

I've use so far the connectivity 0.3.2 in order to check the network connectivity before the async call or login operation. Like the example below :

    checkConnectivity(context) async{

  String connectionStatus;
  StreamSubscription<ConnectivityResult> _connectivitySubscription;
  final Connectivity _connectivity = new Connectivity();
  try {
    connectionStatus = (await _connectivity.checkConnectivity()).toString();
    _connectivity.onConnectivityChanged.listen((ConnectivityResult result) {
      connectionStatus = result.toString();
      //   print(connectionStatus);
    });
  } on PlatformException catch (e) {
    print(e.toString());
    connectionStatus = 'Failed to get connectivity.';
  }
  if(connectionStatus == "ConnectivityResult.none"){
    components.alertPopup(context, "No Internet Connection available" , "Please check your internet connection and try again");}

}

I want to ask if there is any possible way to continuously check internet disconnection in every moment that the user is using the application (even if he is just reading data, without making an API call at all).

In order to notify the user that he is offline with a SnackBar for example.

like image 882
nike Avatar asked Nov 29 '18 11:11

nike


People also ask

How do you check internet disconnection in flutter?

In the body of the Scaffold widget, check if the connection status is false . If it is, we display a text box to the user telling them there is no internet connection. If isn't, we then display our data.

How do you check internet on flutter with GETX?

First we will convert our whole app which MaterialApp to GetMatrialApp so that we can use Getx as global context in our app. return GetMaterialApp( initialBinding: ControllerBinding(), title: 'Internet Connectivity Check (Using Getx)', theme: ThemeData( primarySwatch: Colors. blue, ), home: HomeScreen(), );


1 Answers

You've already written the code to do what you want. You could easily wrap it in a page State or an InheritedWidget or some other managerial class.

final Connectivity _connectivity;
final StreamSubscription<ConnectivityResult> _subscription;

ConstructorForWhateverClassThisIs() {
    _connectivity = new Connectivity();
    _subscription = _connectivity.onConnectivityChanged.listen(onConnectivityChange);
}

void onConnectivityChange(ConnectivityResult result) {
    // TODO: Show snackbar, etc if no connectivity
}

void dispose() {
    // Always remember to cancel your subscriptions when you're done.
    subscription.cancel();
}

According to the documentation, the onConnectivityChanged will be updated with the new result whenever it changes, meaning you can just listen for changes without ever having to manually query it.

Documentation excerpt:

You can also listen for network state changes by subscribing to the stream exposed by connectivity plugin

like image 117
Alex Meuer Avatar answered Oct 28 '22 05:10

Alex Meuer