I have a network call to execute But i need to check before using it if it has internet access

This is what i've done so far

  var connectivityResult = new Connectivity().checkConnectivity();// User defined class
    if (connectivityResult == ConnectivityResult.mobile ||
        connectivityResult == ConnectivityResult.wifi) {*/
    this.getData();
    } else {
      neverSatisfied();
    }

The method above isn't working

Best Answer


The connectivity plugin states in its docs that it only provides information if there is a network connection, but not if the network is connected to the Internet

Note that this doesn't guarantee a connection to the internet on android For instance, the app might have wifi access but it might be a vpn or a hotel wifi with no access.

You can use

import 'dart:io';
...
try {
  final result = await InternetAddress.lookup('example.com');
  if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
    print('connected');
  }
} on SocketException catch (_) {
  print('not connected');
}