简体   繁体   English

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

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

I am trying to display some static html in an app using the webview_flutter plugin.我正在尝试使用webview_flutter插件在应用程序中显示一些静态 html。

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

I was getting an error about an invalid character error, and I assumed that is because Uri defaults to ASCII.我收到一个关于无效字符错误的错误,我认为这是因为 Uri 默认为 ASCII。 I am trying to set the character encoding to UTF-8 but I can't figure out how to do it.我正在尝试将字符编码设置为 UTF-8,但我不知道该怎么做。 Encoding('utf-8') is obviously not right. Encoding('utf-8')显然不对。

How do I set the encoding?如何设置编码?

You can get the encoding like this: 您可以像这样获得编码:

Encoding.getByName('utf-8')

See also How to render a local HTML file in Flutter 另请参见如何在Flutter中呈现本地HTML文件

Safe Url Encoding in flutter颤振中的安全 URL 编码
Ex.前任。

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

In Case of get request :在获取请求的情况下:

Uri.encodeComponent(url+postDataKey+postData);

In Case of Post Data Request use flutter_inappwebview library在 Post Data Request 的情况下使用flutter_inappwebview

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM