Flutter:报错Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform

使用HttpClient进行网络请求,其中请求的url地址是http开头的,flutter 报错 DioError [DioErrorType.DEFAULT]: Bad state: Insecure HTTP is not allowed by platform。

错误解释:平台不支持不安全的 HTTP 协议,即不允许访问 HTTP 域名的地址。

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

解决方案:

iOS:找到Info.plist 文件,修改下列属性:

info.plist

安卓:

1. 为 android 的清单文件 AndroidManifest.xml 的 application 标签内添加两个属性

1.1 AndroidManifest.xml 路径为:android/app/src/main/AndroidManifest.xml) 

1.2 添加的属性为

      android:usesCleartextTraffic="true"

      android:networkSecurityConfig="@xml/network_security_config"

2

2. 添加 network_security_config.xml 文件

2.1 在 android/app/src/main/res 下新建 xml 文件夹

2.2 在 xml 文件夹下创建 network_security_config.xml 文件

2.3 network_security_config.xml 文件内容为:

   

       

           

       

   


参考博文:https://blog.csdn.net/weixin_44137575/article/details/109045633

你可能感兴趣的:(Flutter:报错Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform)