Reputation: 85
I am trying to check for internet connection before making an api request. Following is my helper function to check internet
Future<bool> checkInternetConnection() async {
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
return true;
}
} on SocketException catch (_) {
return false;
}
return false;
}
The helper function above works properly when in debug mode for Android, but for release mode in Android it returns false even when internet connection is available. I tried with both Wifi and Mobile data. The above functions works properly in iOS.
Upvotes: 2
Views: 1849
Reputation: 5736
You need to add Internet permission in your-flutter-app/android/app/src/main/AndroidManifest.xml
in android
directory of Flutter app. Your app works in debug mode because Internet permission must be there in your-flutter-app/android/app/src/debug/AndroidManifest.xml
<!-- Add this in src/main/AndroidManifest.xml inside android directory of Flutter app -->
<uses-permission android:name="android.permission.INTERNET"/>
Upvotes: 5