43

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.

3
  • 1
    Care to read how to enable http network request by following the same guide link that you have shared with your post. Commented Oct 2, 2020 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. Commented Oct 2, 2020 at 14:12
  • Official doc : flutter.dev/docs/release/breaking-changes/… Commented Jun 29, 2021 at 7:34

12 Answers 12

71

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>
Sign up to request clarification or add additional context in comments.

4 Comments

For those testing on iOS and wondering where to put code in Info.plist; place the code in the iOS answer between the <plist><dict></dict></plist> tags. Also, double check that your hosts firewall is not enabled.
What's about Flutter Web?
@PavelKalashnikov, As far as I know Flutter web should support http and https without modification. These are both supported by browsers by default.
@Suragch thanks! Figured out yesterday, that my browser redirected all requests localhost:3000 to localhost:3000, and it was not flutter's fault. So, I decided to use ngrok to be able to use https for my local server.
11

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?

Comments

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

Comments

6

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>

Comments

6

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.

Comments

6

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

Comments

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" />

Comments

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

1 Comment

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
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.

Comments

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.

Comments

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

1 Comment

how to pass the link of API like eg "google.com"
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 !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.