Custom Action to Check Internet Connection

This custom action will check if there is internet connection in the device. The action will return a boolean value.

Step 1:

Add a custom action and name it 'checkInternetConnection'.

Step 2:

Just copy the code below, paste it, compile it, save it.

import 'dart:io';
import 'dart:async';

Future<bool> checkInternetConnection() async {
  // Check internet connectionn
  try {
    final result = await InternetAddress.lookup('google.com');
    if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
      return true;
    }
  } catch (e) {
    return false;
  }
  return false;
}

22
1 reply