【问题标题】:How to fix 'net::ERR_CLEARTEXT_NOT_PERMITTED' in flutter如何在颤动中修复“net::ERR_CLEARTEXT_NOT_PERMITTED”
【发布时间】:2019-08-30 17:20:07
【问题描述】:

我已经在 Flutter 中实现了 webView,但它没有打开我在服务器上的 php 网站,这是我做错了。

我是 Flutter 的新手,并尝试使用 webview 将我的网站网页集成到我的应用程序中,但没有成功。

Widget build(BuildContext context) {
    // TODO: implement build
    return WebviewScaffold(
      appBar: AppBar(iconTheme:IconThemeData(color: Colors.white),title: Text("Intake Form",style:new TextStyle(color: Colors.white,fontWeight: FontWeight.bold)),backgroundColor:Colors.indigoAccent,automaticallyImplyLeading: false),
     url: url,
      //url: "http://xxxxxxxx/",
       withJavascript: true,
       supportMultipleWindows: true,
      withLocalStorage: true,
      allowFileURLs: true,
      enableAppScheme: true,
      appCacheEnabled: true,
      hidden: false,
      scrollBar: true,
      geolocationEnabled: false,
      clearCookies: true,
       // usesCleartextTraffic="true"



    );
  }

我希望输出为运行 webview 但抛出错误。

【问题讨论】:

    标签: flutter flutter-dependencies


    【解决方案1】:

    在您的 AndroidManifest 文件中将 usesCleartextTraffic 属性设置为 true,如下所示。

    <application
    ....
    android:usesCleartextTraffic="true"
    ....>
    

    【讨论】:

    • 即使使用此选项,它也不起作用:/ 我必须运行 flutter clean 然后再次重新运行项目
    • @lonelLupu 可能不需要清洁。似乎停止整个应用程序并 rebuild 就足够了。原因是,AndroidManifest 不会随着 Flutter 的热重载/重启而重载。
    【解决方案2】:

    打开android清单文件(android/app/src/main/AndroidManifest.xml)并添加

    android:usesCleartextTraffic="true" 到应用程序标签

    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="tangerine_ui"
        android:icon="@mipmap/ic_launcher"
        android:usesCleartextTraffic="true">
    

    【讨论】:

      【解决方案3】:

      在AndroidManifest.xml中,添加[android:usesCleartextTraffic="true"]为

      <application
          ......
          .......
          android:usesCleartextTraffic="true"
          .............. >
          <.........
              ................... />
      
                  ..........
          ...........>
      </application>
      

      在 android 版本 9 中无法正常工作

      【讨论】:

      • 奇怪的是,它在我的 Android 9 (Samsung S10e) 上运行
      【解决方案4】:
      • 制作 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>

      • 在Manifest文件的Application标签中添加这两行。

      【讨论】:

      • 这个文件需要放在哪里?
      • 在 res 文件夹中制作 xml 文件夹,然后放入这个 .xml 文件。
      • 我在 Flutter 中工作,所以里面没有 res 文件夹。
      【解决方案5】:

      在 Flutter 项目的主目录中,您有三个主要文件夹:

      - lib         =  your Dart code
      - ios         =  generated structure for iOS platform
      - android     =  generated structure for Android platform
      

      我们对android 目录感兴趣。 当你打开它时,你会看到“典型的 Android 应用结构”。

      所以你必须做两件事:

      1) 在res 中添加新文件

      进入目录:

      my_flutter_project/android/app/src/main/res/
      

      创建xml目录(在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" />
                  <certificates src="user" />
              </trust-anchors>
          </base-config>
      </network-security-config>
      

      network_security_config.xml 应该位于路径:

      my_flutter_project/android/app/src/main/res/xml/network_security_config.xml
      

      您可以在此处找到有关此文件的更多信息:

      https://developer.android.com/training/articles/security-config

      2) 修改AndroidManifest.xml

      转到:

      flutter_project/android/app/src/main/AndroidManifest.xml
      

      AndroidManifest.xml是XML文件,结构:

      <manifest>
          <application>
              <activity>
                  ...
              </activity>
              <meta-data >
          </application>
      </manifest>
      

      所以对于&lt;application&gt; PROPERTIES,您必须添加 1 行:

      android:networkSecurityConfig="@xml/network_security_config"
      

      请记住,您必须将其添加为属性(在application 开始标记内):

      <application
      
          SOMEWHERE HERE IS OK
      
      >
      

      不作为标签:

      <application>           <--- opening tag
      
          HERE IS WRONG!!!!
      
      <application/>          <--- closing tag
      

      【讨论】:

      • 每一步的解释都非常清晰,真是一个绝妙的答案!
      • 我不明白为什么主题启动器没有接受这个答案。真的行!谢谢,并接受我的投票
      • 答案是正确的,这是我根据问题的看法是,当开发人员使用 http:// 实现站点时,如果您只是使用 https:// 更改 URL,则需要此安全文件:/ / 你不会在 web 视图中得到任何错误
      【解决方案6】:

      对于 ios 修改 info.plist ./ios/Runner/info.plist

      添加以下内容:

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

      在再次启动应用程序之前运行flutter clean

      【讨论】:

        【解决方案7】:

        安卓:

        在flutter_project/android/app/src/main/AndroidManifest.xml中

        添加

        <application
        ....
        android:usesCleartextTraffic="true"
        ....>
        

        IOS:

        在./ios/Runner/info.plist中

        添加

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

        运行flutter clean。然后,运行应用程序。

        【讨论】:

          猜你喜欢
          • 2022-01-24
          • 2020-12-23
          • 1970-01-01
          • 1970-01-01
          • 2022-11-02
          • 2023-01-02
          • 1970-01-01
          • 2020-07-06
          • 1970-01-01
          相关资源
          最近更新 更多