如何在 Flutter 项目中为 dart Uri 指定编码

How to specify the encoding for a dart Uri in a Flutter project

我正在尝试使用 webview_flutter 插件在应用程序中显示一些静态 html。

body: WebView(
  initialUrl: Uri.dataFromString(
      htmlString, 
      mimeType: 'text/html', 
      encoding: Encoding('utf-8')
  ).toString(),
),

我收到有关无效字符错误的错误,我认为这是因为 Uri 默认为 ASCII。我正在尝试将字符编码设置为 UTF-8,但我不知道该怎么做。 Encoding('utf-8')显然不对。

如何设置编码?

你可以得到这样的编码:

Encoding.getByName('utf-8')

另见 How to render a local HTML file in Flutter

安全Url flutter 编码
例如

String url  = 'http://example.org/';
String postDataKey = "requestParam="
String postData = 'hdfhghdf+fdfbjdfjjndf'

在获取请求的情况下:

Uri.encodeComponent(url+postDataKey+postData);

如果 Post 数据请求使用 flutter_inappwebview

var data = postDataKey + Uri.encodeComponent(postData);
webViewController.postUrl(url: Uri.parse(url), postData: utf8.encode(data));