2

I am trying to display some static html in an app using the webview_flutter plugin.

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. I am trying to set the character encoding to UTF-8 but I can't figure out how to do it. Encoding('utf-8') is obviously not right.

How do I set the encoding?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393

2 Answers2

2

You can get the encoding like this:

Encoding.getByName('utf-8')

See also How to render a local HTML file in Flutter

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
1

Safe Url Encoding in flutter
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

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