Suragch
Suragch

Reputation: 511656

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.

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?

Upvotes: 2

Views: 4000

Answers (2)

dinesh_ghadge
dinesh_ghadge

Reputation: 21

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));

Upvotes: 1

Suragch
Suragch

Reputation: 511656

You can get the encoding like this:

Encoding.getByName('utf-8')

See also How to render a local HTML file in Flutter

Upvotes: 2

Related Questions