【问题标题】:Bad state: Insecure HTTP is not allowed by platform: [closed]不良状态:平台不允许使用不安全的 HTTP:[关闭]
【发布时间】:2021-01-19 16:15:43
【问题描述】:

我遇到以下问题: E/flutter(7144):[错误:flutter/lib/ui/ui_dart_state.cc(177)]未处理的异常:错误状态:平台不允许不安全的HTTP:http://myIPv4:PORT/PATH。

这是我可以访问的后端:

我已经允许通过 cors 访问,但这对我没有帮助。 我已经尝试使用 http://localhost:port/path 和 http://myIP:port/path 但没有用!

但如果我尝试通过浏览器直接访问,那么工作。

【问题讨论】:

  • var response = await http.get(http://IPV4:port/); 试试这样

标签: node.js flutter http mobile connection


【解决方案1】:

这个问题到这里就解决了。

https://flutter.dev/docs/release/breaking-changes/network-policy-ios-android

您只需将 HTTP 更改为 HTTPS。 像这样

Response response = await get('https://worldtimeapi.org/api/timezone/Africa/Nairobi');

【讨论】:

  • 这对我有用!谢谢。
【解决方案2】:
  1. 导航到您的项目。
  2. 转到您的应用\android\app\src\debug\AndroidManifest.xml。
  3. 添加此行。
<application android:usesCleartextTraffic="true">
</application>

更多详情,请关注this链接。

【讨论】:

  • 它仅适用于安卓系统。 ios的解决方法是什么?
  • 在我的回答中给出了解决方案。请检查@DhirajBaruah
  • 如果您使用的是本地服务器,则此方法有效。谢谢你
【解决方案3】:

我按照这个步骤解决了:

  1. android/app/src/main/AndroidManifest.xml 然后写

    <application
    android:usesCleartextTraffic="true"
    android:label="laundry"
    android:icon="@mipmap/ic_launcher">
    <uses-library
     android:name="org.apache.http.legacy"
     android:required="false" />  </application>
    

【讨论】:

    【解决方案4】:

    对于安卓:

    按照迁移指南可以省略此行为:https://flutter.dev/docs/release/breaking-changes/network-policy-ios-android

    或者...只需添加 android/app/src/main/AndroidManifest.xml:

    android:usesCleartextTraffic="true"&lt;application /&gt; 也不要忘记获取 INTERNET 许可:

    <uses-permission android:name="android.permission.INTERNET" /> <!-- This Line -->
    
    <application
            android:name="io.flutter.app.FlutterApplication"
            android:label="receipt"
            android:usesCleartextTraffic="true" <!-- This Line -->
            android:icon="@mipmap/ic_launcher">
    

    对于 iOS:

    在 iOS 上的应用程序中全局允许不安全/HTTP 请求,您可以将其添加到主字典定义下的 ios/Runner/info.plist

    <key>NSAppTransportSecurity</key>
    <dict>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
    </dict>
    

    请注意,启用此功能时您需要向 Apple 的审核团队解释,否则您的应用将在提交时被拒绝。

    谢谢。

    【讨论】:

    • 添加 iOS 代码时出现错误。您能否详细说明如何在 info.plist @Pratik Butani 中添加它
    • 如果还是不行,请更新 Flutter 和 android studio。
    • 如果在某些时候无法正常工作,请关闭应用并重新构建。
    • 编辑AndroidManifest.xml(在debug和main目录下)后不要忘记重启模拟器。它没有包含在 Flutter Hot Reload 中;)
    猜你喜欢
    • 2021-07-30
    • 2021-08-11
    • 2021-07-18
    • 2021-07-16
    • 2021-01-17
    • 2021-01-18
    • 2017-06-15
    • 2021-06-04
    • 2018-12-07
    相关资源
    最近更新 更多