Reputation: 643
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.
Upvotes: 43
Views: 68554
Reputation: 174
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.
Upvotes: 2
Reputation: 31
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" />
Upvotes: 3
Reputation: 2470
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:
Upvotes: 6
Reputation: 126
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
Upvotes: 1
Reputation: 511626
Generally it is required (and preferable) to use https links rather than http links. However, this can be overridden as shown below.
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.
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.
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>
Upvotes: 69
Reputation: 544
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.
Upvotes: 6
Reputation: 706
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.
Upvotes: 2
Reputation: 122
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
Upvotes: 2
Reputation: 3253
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>
Upvotes: 8
Reputation: 38621
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?
Upvotes: 11
Reputation: 524
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 !
Upvotes: 0
Reputation: 327
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>
Upvotes: 6