Check Internet Connection on Flutter app

I have a network call to be executed. But before doing that I need to check whether the device have internet connectivity.

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 on Android, this does not guarantee connection to Internet. For instance, the app might have wifi access but it might be a VPN or a hotel WiFi with no access.

Use this code:

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');
}

Also Read:

We will be happy to hear your thoughts

Leave a reply

CodeROG
Logo
Compare items
  • Total (0)
Compare
0