Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. Bad state: Insecure HTTP is not allowed by platform in a flutter. So today Here I come with all possible solutions for this error.
We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.
How Bad state: Insecure HTTP is not allowed by platform Error Occurs ?
I am trying to get data from API. but my API url is http. So when I run my app having the following problem:
E/flutter ( 7144): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform
How to Solve Bad state: Insecure HTTP is not allowed by platform Error?
- How to Solve Bad state: Insecure HTTP is not allowed by platform Error?
To solve Bad state: Insecure HTTP is not allowed by platform Flutter Officially announced that Insecure HTTP connections are disabled by default on iOS and Android. Open your android/app/src/main/AndroidManifest.xml. Add below line in between your
android:usesCleartextTraffic=”true” Also add Internet Permission. flutter clean Flutter run - Bad state: Insecure HTTP is not allowed by platform Error
To solve Bad state: Insecure HTTP is not allowed by platform Flutter Officially announced that Insecure HTTP connections are disabled by default on iOS and Android. Open your android/app/src/main/AndroidManifest.xml. Add below line in between your
android:usesCleartextTraffic=”true” Also add Internet Permission. flutter clean Flutter run
Solution 1
Flutter Officially announced that Insecure HTTP connections are disabled by default on iOS and Android
1. For Android
- Open your
android/app/src/main/AndroidManifest.xml
. - Add below line in between your <application></application>
- android:usesCleartextTraffic=”true”
- Also add Internet Permission.
- <uses-permission android:name=”android.permission.INTERNET” />
- flutter clean
- Flutter run
2. For iOS
Allow insecure/HTTP requests globally across your application on iOS, you can add this to your ios/Runner/info.plist under the main dictionary definition:
Be warned that you will need to have an explanation for Apple’s review team when enabling this, otherwise your app will get rejected on submission.
- Open your ios/Runner/Info.plist
- Add below line in info.plist file.
<key>NSAppTransportSecurity</key>
<<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
- Flutter clean
- Flutter run.
Also watch My full video for batter understanding.
Solution 2
- Navigate to your project.
- Go to yourapp\android\app\src\debug\AndroidManifest.xml.
- Add this line.
<application android:usesCleartextTraffic="true">
</application>
Solution 3 : 100% Working
If Above 2 Not Worked then 100% This solution will work
- Go to this path: yourProject\android\app\src\main\res
and create a folder named XML
- open this path: yourProject\android\app\src\main\res\xml and create xml file named network_security_config.xml
- 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>
- 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>
- stop and restart your application and it will work with you
Summery
So, It’s All About this error. I hope this tutorial helps you to solve your error. Please Comment Below if You stucks anywhere with my code.
Also Check Out Below Tutorials
- Flutter: How to fix “A RenderFlex overflowed by pixels ” error?
- No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider
- The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’
- RangeError (index): Invalid value: Valid value range is empty: 0
- Gradle failure A problem occurred evaluating project ‘:app’
I tried all 3 ways above, but I still have this error in debug mode, do you have another way? Thank you so much
Can you tell me Thu Tran in which Platform you are using Android, iOS OR web?
can you show me AndroidManifest.xml and Info.plist file screenshot ?