2

How can I convert string to HTML encoded string in Flutter.

This is an example string

This is the sample text & special character 
also new line

I want to convert like this

This is the sample text &amp; special character <br> also new line

Not only &amp; I want all of the HTML entities available. I found HTML to string everywhere But not String to HTML anywhere! I want reversed/opposite feature what html_unescape: provide!

2 Answers 2

3

I think this is what you're looking for:

https://api.flutter.dev/flutter/dart-convert/HtmlEscape-class.html

Sign up to request clarification or add additional context in comments.

Comments

1

Check out how to do it with the Dart HtmlEscape class.

const HtmlEscape htmlEscape = HtmlEscape();
String unescaped = 'Text & subject'; // The text you want to convert
String escaped = htmlEscape.convert(unescaped);
print(escaped); // Text &amp; subject

Using flutter_html to preview with HTML template.

import 'package:html/parser.dart' as htmlparser;
import 'package:html/dom.dart' as dom;
...
String htmlData = """<div>
  <h1>Demo Page</h1>
  <p>This is a fantastic product that you should buy!</p>
  <h3>Features</h3>
  <ul>
    <li>It actually works</li>
    <li>It exists</li>
    <li>It doesn't cost much!</li>
  </ul>
  <!--You can pretty much put any html in here!-->
</div>""";
dom.Document document = htmlparser.parse(htmlData);
/// sanitize or query document here
Widget html = Html(
  document: document,
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.