how to add hyperlink to text in dart

515 Views Asked by At

I am trying to add an anchor tag to the message I want to display but I am getting an error message. Am I not doing this right?

String browserRequirementsUrl = "https://test.testing.com";
var someText = new ParagraphElement()
  ..innerHtml = "Link can be found <a href=${url}>here</a>[1].";

But I get an error message saying

  html_dart2js.dart:3614 Removing disallowed attribute <A href="https://test.testing.com">

Any suggestions, how I can do this?

1

There are 1 best solutions below

0
On

By default in dart:html, for security purposes, this is disallowed.

You can use the .setInnerHtml method:

..setInnerHtml("Link can be found...", treeSanitizer: NodeTreeSanitizer.trusted);

Note that this can potentially be insecure (i.e. inject <script> tags and such), so you can always create a custom sanitizer or validator to only allow a subset of HTML tags (such as <a>).