flutter - 如何在 flutter 中修复 'net::ERR_CLEARTEXT_NOT_

我已经在 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 项目的主目录中,您有三个主要文件夹:

- 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

Here you can find more information about this file:

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>

所以对于 <application> PROPERTIES 你必须添加 1 行:

android:networkSecurityConfig="@xml/network_security_config"

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

<application

    SOMEWHERE HERE IS OK

>

不作为标签:

<application>           <--- opening tag

    HERE IS WRONG!!!!

<application/>          <--- closing tag

关于flutter - 如何在 flutter 中修复 'net::ERR_CLEARTEXT_NOT_PERMITTED',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592392/

相关文章:

android - Gradle 失败可能是因为这个 Flutter 应用程序中的 AndroidX

dart - 在 Flutter 中,在 pubspec.yaml 文件中添加包的最佳方法是什么?

flutter - 在 Dart 中添加/减去迄今为止的月/年?

unicode - 在 Dart 中处理字素簇

http - flutter http header

android - 长按时 flutter 触觉反馈

firebase - flutter firestore,在数组中添加新对象

dart - 如何在 dart 中使用 request.send() 获取响应正文

flutter - 使用 FutureBuilder 时重新加载数据

flutter - 在 flutter 中将数据作为对象存储在共享首选项中