38

Flutter team recently made this change and now insecure http connections are not allowed. https://flutter.dev/docs/release/breaking-changes/network-policy-ios-android

I would like to know how can I connect my flutter app on mobile to local go server running on my PC.

My server is running on: http://192.168.29.45:4001 but it is not connecting to it.

Raghav Aggiwal
  • 573
  • 2
  • 6
  • 16
  • 1
    Care to read how to enable http network request by following the same guide link that you have shared with your post. – OMi Shah Oct 02 '20 at 14:03
  • You can allow insecure connections only to domains. Specific IP addresses are not accepted as input. This is in line with what platforms support. – Raghav Aggiwal Oct 02 '20 at 14:12
  • Official doc : https://flutter.dev/docs/release/breaking-changes/network-policy-ios-android – Kab Agouda Jun 29 '21 at 07:34

12 Answers12

67

Generally it is required (and preferable) to use https links rather than http links. However, this can be overridden as shown below.

Android

Open the AndroidManifest.xml file in the android/app/src/main folder. Then set usesCleartextTraffic to true.

<application
    ...
    android:usesCleartextTraffic="true"
    ...   >

See this question for more.

iOS

Open the Info.plist file in the ios/Runner folder. Then add the following key.

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

See this answer for more.

macOS

This is the same as iOS. Open the Info.plist file in the macos/Runner folder. Then add the following key.

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • For those testing on iOS and wondering where to put code in `Info.plist`; place the code in the iOS answer between the `` tags. Also, double check that your hosts firewall is not enabled. – shennan May 19 '21 at 11:51
10

As I have already answered. To temp fix your problem. First add the following config in the file named network_security_config inside res/xml directory(my full path is /Users/dolphin/source/third-party/Cruise/android/app/src/main/res/xml/network_security_config):

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

then in your AndroidManifest.xml file, inside <application tag, add the following config:

android:networkSecurityConfig="@xml/network_security_config"

this could allow http traffic. But it just using to debug in your local env or test env, the best way is to using https traffic. more info: How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?

Dolphin
  • 29,069
  • 61
  • 260
  • 539
8

The easy way to implement this is to use this attribute inside your AndroidManifest.xml this will allow http traffic:

<application android:usesCleartextTraffic="true">
</application>

More info here

Mateen Kiani
  • 2,869
  • 2
  • 19
  • 29
5

In android/app/AndroidManifest

<application ...
  android:networkSecurityConfig="@xml/network_security_config" >

 <meta-data android:name="io.flutter.network-policy"
        android:resource="@xml/network_security_config"/>

Then create a folder xml inside create a file: network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <base-config cleartextTrafficPermitted="true">
     <trust-anchors>
        <certificates src="system" />
    </trust-anchors>
  </base-config>
 </network-security-config>
Bruno Camargos
  • 189
  • 1
  • 5
5

The easiest solution for me was to add the following line to my DEBUG manifest file which can be found in $project_path\android\app\src\debug\AndroidManifest.xml:

<application android:usesCleartextTraffic="true"/>

IMPORTANT - It didn't work until I restarted the emulator, so make sure you don't skip this step.

Daniel
  • 452
  • 6
  • 14
5

The easy way:

Android

Go to android/app/src/main/AndroidManifest.xml. Open AndroidManifest.xml file. Then set android:usesCleartextTraffic="true" inside

Example:

    <uses-permission android:name="android.permission.INTERNET" /> //This line add
    <application
        android:name="io.demo.app.test"
        android:label="ProjectName"
        android:usesCleartextTraffic="true" //This line add
        android:icon="@mipmap/ic_launcher">

iOS

Go to ios/Runner/info.plist under the main dictionary and open info.plist set this lines of code:

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

After complete this steeps then Close this app and run:

  • flutter clean
  • flutter pub get
  • flutter run
MD MEHEDI HASAN
  • 2,044
  • 2
  • 19
  • 34
3

I don't know people are saying to change the Android Manifest file in the app/src/main/AndriodManifest.xml, when the tutorial on the flutter webiste explicity says :

If you would like to allow HTTP connections for Android debug builds, you can add the following snippet to your $project_path\android\app\src\ debug\AndroidManifest.xml:

< application android:usesCleartextTraffic="true" />

Achal Jain
  • 31
  • 1
2

1- Go to this path: yourProject\android\app\src\main\res
and create folder named xml

2 - open this path: yourProject\android\app\src\main\res\xml and create xml file named network_security_config.xml

3 - inside network_security_config.xml file write this:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

4- You need to go to this path: yourProject\android\app\src\main\AndroidManifest.xml:

<application
    android:usesCleartextTraffic="true"

    <activity>
         <meta-data android:name="io.flutter.network-policy"
              android:resource="@xml/network_security_config"/>
     </activity>
 </application>

5- stop and restart your application and it will work with you

Ahmed
  • 102
  • 2
  • 8
  • 1
    can you check step 4 code? I am getting error over there `Missing 'name' key attribute on element activity at AndroidManifest.xml:46:5-49:17` – Dhanraj Nimbalkar Apr 15 '21 at 13:41
2

If you've tried all solutions above but they didn't fix it, you should close the emulator and open it again because changes in gradle are important changes so they require it. You need to close the emulator and open it again to see the results of above solutions.

batuhankrbb
  • 598
  • 7
  • 10
2

Add this to android\app\src*debug*\AndroidManifest.xml file:

<application android:usesCleartextTraffic="true">
</application>

Don't add to android\app\src*MAIN*\AndroidManifest.xml they are two different things. Then Stop and Rebuild the app, because Hot Restart and Hot Reload Won't Work then All the errors will be gone.

1

I upgraded the flutter HTTP library into latest version https://pub.dev/packages/http

http: ^0.13.1

and then change the URI declaration as HTTPS instead of HTTP

    final http.Response response = await http.post(
        Uri.https('<IP_ADDRESS>', ''),

        headers: <String, String>{
            '<YOUR_HEADERS>',
        },
        body: jsonEncode(<String, Object>{
            '<REQUEST_BODY>',
        }),
    );

This solved the problem for me

0

I was using

final String baseUrl='http://......herokuapp.com/';

this url that was giving me this exception but then i did http to https

final String baseUrl='https://......herokuapp.com/';

that's due to security purpose of some mobiles

and that worked for me thanks in advance !

Vaibhav Pallod
  • 466
  • 7
  • 21