Flutter - Insecure socket connections are disallowed by platform: 10.0.2.2
Asked Answered
M

4

11

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

Manhood answered 2/9, 2020 at 11:14 Comment(0)
R
13

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"
Rumor answered 4/9, 2020 at 17:22 Comment(0)
E
2

First add-in AndroidManifest file

add this line in the end before closing tag

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

add this line in the beginning find this tag and add it first in it

<application
    android:usesCleartextTraffic="true"
Edwardedwardian answered 2/9, 2020 at 18:44 Comment(0)
A
0

It seems you newly upgraded to Android 10, Well you probably didn't know that HTTPS is the default connection protocol starting with Android 9 and that all connection not using Https will fail. Read more https://developer.android.com/training/articles/security-config.html

See this answer here https://mcmap.net/q/35675/-android-8-cleartext-http-traffic-not-permitted

Alps answered 2/9, 2020 at 12:0 Comment(0)
C
0

I was making a call to- Response response = await get('http://worldtimeapi.org/api/timezone/Asia/Kolkata');

Instead of http I simply replaced it to https and it was fixed.

The new request was- Response response = await get('https://worldtimeapi.org/api/timezone/Asia/Kolkata');

This fixed the issue for me.

Cede answered 19/9, 2020 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.