Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Insecure socket connections are disallowed by platform: 10.0.2.2

Since today whenever I try to login my application I get the following error thrown by the Dio package: SocketException: Insecure socket connections are disallowed by platform: 10.0.2.2

I use the following settings to connect:

static BaseOptions options = new BaseOptions(
    baseUrl: "http://10.0.2.2:3000", // on android emulator
    connectTimeout: 5000,
    receiveTimeout: 3000)

And consequently something along the lines of (where I have authentication set-up and functioning properly at /user/login):

var apiLogin = api.dio;
try {
Response response = await apiLogin.post("/user/login",
          options: Options(contentType: "application/json"),
          data: {"email": email, "password": password});
} on DioError catch (e) {
throw Exception([e]);
} 

I have a Node server running on port 3000 which is connected to (containerized) mongodb. When trying the authentication, it immediately has the DioError and I haven't been able to find the cause anywhere online.

Does anyone know what this error is related to?

EDIT [ANSWER]

Thanks to @lyrics for pointing me in the right direction: From API level 27 and higher, usesCleartextTraffic defaults to false, consequently blocking outgoing http requests, requiring HTTPS.

The solution was to add the following to AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>

as stated in stackoverflow answer

source: https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic

like image 270
Rowan Arts Avatar asked Sep 02 '20 11:09

Rowan Arts


1 Answers

I have a similar issue with Flutter running on ios Simulator and Android emulator:

SocketException: Insecure socket connections are disallowed by platform:

Inside the Flutter project go to :

To enable in Ios:

ios folder -> runner folder -> info.plist

Then add the following lines to enable HTTP requests:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

To enable in Android:

Android folder -> app -> src -> main -> AndroidManifest.xml

Add this permission:

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

then add the following line inside application tag:

android:usesCleartextTraffic="true"
like image 188
Mina Farid Avatar answered Oct 31 '22 09:10

Mina Farid