flutter 2.X报错 Bad state: Insecure HTTP is not allowed by platform:

flutter2.x开发遇到的问题 Bad state: Insecure HTTP is not allowed by platform:

翻译过来就是:错误状态:平台不允许不安全的HTTP:

产生原因:IOS 和 Android 9.0 对网络请求做了一些限制,不能直接访问 Http 域名的地址。

解决方案如下:

Android 配置

找到下图位置,添加这两行代码

      android:usesCleartextTraffic="true"
      android:networkSecurityConfig="@xml/network_security_config"

flutter 2.X报错 Bad state: Insecure HTTP is not allowed by platform:

flutter 2.X报错 Bad state: Insecure HTTP is not allowed by platform:

添加 network_security_config.xml 文件,没有这个文件就创建一个,注意一下路径哦

android\app\src\main\res\xml\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>

flutter 2.X报错 Bad state: Insecure HTTP is not allowed by platform:

IOS 配置

找到 ios/Runner/Info.plist,添加关于 NSAppTransportSecurity NSAllowsArbitraryLoads 的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>NSAppTransportSecurity</key> 
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<true/>
	</dict>
</dict>
</plist>

flutter 2.X报错 Bad state: Insecure HTTP is not allowed by platform:

搞完以后,重启项目!!重启重启重启重启!重启解决一切问题!!!!

下面的可能用不到,一般上面的都可以解决问题了哦~~~

备注:网上还查资料看到network_security_config.xml有其他配置办法

此办法参考链接:https://www.cnblogs.com/sea-stream/p/13908198.html

补充如下

network_security_config.xml

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

or

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>

<!-- 配置7.0抓包--start -->
<debug-overrides>
    <trust-anchors>
        <!-- Trust user added CAs while debuggable only -->
        <certificates src="user"/>
    </trust-anchors>
</debug-overrides>
<!-- 配置7.0抓包--end -->

<!-- 配置9.0明文请求--start -->
<base-config cleartextTrafficPermitted="true" />
 <!-- 配置9.0明文请求--end -->   

</network-security-config>
上一篇:非服务器模式下运行getImageData函数出现 the operation is insecure


下一篇:关于HTTP请求首部字段中 Upgrade-Insecure-Requests:1的理解