close
Bad state: Insecure HTTP is not allowed by platform in a flutter, Bad state: Insecure HTTP is not allowed by platform in android, Bad state: Insecure HTTP is not allowed by platform in ios, Bad state: Insecure HTTP is not allowed by platform, Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform

[Solved] Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform in a flutter

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?

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

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

  1. Open your android/app/src/main/AndroidManifest.xml.
  2. Add below line in between your <application></application>
  3. android:usesCleartextTraffic=”true”
  4. Also add Internet Permission.
  5. <uses-permission android:name=”android.permission.INTERNET” />
  6. flutter clean
  7. 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

  1. Navigate to your project.
  2. Go to yourapp\android\app\src\debug\AndroidManifest.xml.
  3. 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


Posted

in

by

Comments

2 responses to “[Solved] Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform in a flutter”

  1. Thu Tran Avatar
    Thu Tran

    I tried all 3 ways above, but I still have this error in debug mode, do you have another way? Thank you so much

    1. Milan Dhameliya Avatar

      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 ?

Leave a Reply

Your email address will not be published. Required fields are marked *