如何在颤抖中修复'net :: ERR_CLEARTEXT_NOT_PERMITTED'

Aka*_*wal 4 flutter flutter-dependencies

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

我是陌生的,尝试使用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://61.246.39.79:8080/",
       withJavascript: true,
       supportMultipleWindows: true,
      withLocalStorage: true,
      allowFileURLs: true,
      enableAppScheme: true,
      appCacheEnabled: true,
      hidden: false,
      scrollBar: true,
      geolocationEnabled: false,
      clearCookies: true,
       // usesCleartextTraffic="true"



    );
  }
Run Code Online (Sandbox Code Playgroud)

我期望输出为正在运行的webview,但会引发错误。

Bok*_*ken 47

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

- lib         =  your Dart code
- ios         =  generated structure for iOS platform
- android     =  generated structure for Android platform
Run Code Online (Sandbox Code Playgroud)

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

所以你必须做两件事:

1)添加新文件 res

进入目录:

my_flutter_project/android/app/src/main/res/
Run Code Online (Sandbox Code Playgroud)

创建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>
Run Code Online (Sandbox Code Playgroud)

network_security_config.xml 应该位于路径中:

my_flutter_project/android/app/src/main/res/xml/network_security_config.xml
Run Code Online (Sandbox Code Playgroud)

network_security_config

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

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

2)修改AndroidManifest.xml

去:

flutter_project/android/app/src/main/AndroidManifest.xml
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

AndroidManifest.xml 是 XML 文件,结构为:

<manifest>
    <application>
        <activity>
            ...
        </activity>
        <meta-data >
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

因此,对于<application>PROPERTIES,您必须添加 1 行:

android:networkSecurityConfig="@xml/network_security_config"
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

<application

    SOMEWHERE HERE IS OK

>
Run Code Online (Sandbox Code Playgroud)

不作为标签:

<application>           <--- opening tag

    HERE IS WRONG!!!!

<application/>          <--- closing tag
Run Code Online (Sandbox Code Playgroud)

  • 每一步都解释得非常清楚,这是一个多么美妙的答案啊! (23认同)
  • 我不明白为什么主题发起者没有接受这个答案。真的行!谢谢,并投我的一票 (3认同)
  • 答案是正确的,这是我根据问题的看法,当开发人员使用 http:// 实现网站时,则需要此安全文件,否则如果您只是使用 https:// 更改 url,您将不会收到任何错误网页视图 (2认同)

Muk*_*ony 28

安卓:

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

添加

<application
....
android:usesCleartextTraffic="true"
....>
Run Code Online (Sandbox Code Playgroud)

苹果系统:

在 flutter_project/ios/Runner/info.plist 中

添加

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)

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


Sil*_*der 18

打开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">
Run Code Online (Sandbox Code Playgroud)


小智 8

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

<application
....
android:usesCleartextTraffic="true"
....>
Run Code Online (Sandbox Code Playgroud)

  • 即使使用此选项,它也不起作用:/我必须运行“flutter clean”,然后再次重新运行该项目 (5认同)
  • @lonelLupu 可能不需要清洁。似乎停止整个应用程序并“重建”就足够了。原因是,AndroidManifest 不会随着 Flutter 的热重载/重启而重新加载。 (3认同)