androidflutterdartuser-permissions

Why there is internet access issue in some devices of Samsung for flutter?


I am developing a flutter application where I need to check internet connection. But it is working fine for rest of the device except Samsung s23, Galaxy Z Fold5. I am checking the release apk, and do not have any real device access for this I could get any debug result.

In AndroidManifest, I have taken this permission:

<uses-permission android:name="android.permission.INTERNET" />

Here is the code, for checking for an internet connection:

final result = await InternetAddress.lookup('example.com');

try {
    if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        Get.snackbar('Internet', 'Navigate to Internet');
    } else {
        Get.snackbar('No Internet', 'Navigate to No Internet');
    }
} on SocketException catch (_) {
    Get.snackbar('No Internet', 'SocketException');
}

In release mode, it always shows the snackbar from the exception. I need to show the 'Navigate to Internet' snackbar.

Can I get any solution?


Solution

  • It seems there is an issue in the Dart SDK for it. It was closed without a fix.

    One suggestion there is to use the internet_connection_checker_plus package, which seems to do exactly what you're trying to achieve.

    To use it, add the following to your pubspec.yaml:

    dependencies:
      internet_connection_checker_plus: ^2.1.0
    

    And change your code to use it:

    final hasInternet = await InternetConnection().hasInternetAccess;
    
    if (hasInternet) {
        Get.snackbar('Internet', 'Navigate to Internet');
    } else {
        Get.snackbar('No Internet', 'Navigate to No Internet');
    }