In Dart must element.html(" ") occur within the context of a body tag?


TableElement table = new TableElement();
table.children.add(new Element.html("<tr><td>test</td></tr>"));

The above code snippet returns the error: “Uncaught Bad state: No element”. Reading the API docs on the DartLang website this is because when using the Element.html(” “) constructor, “the HTML fragment is parsed as if it occurred within the context of a <body> tag”. Therefore, we would need to write:

new Element.html("<table><tr><td>test</td></tr></table>")

However, reading “dart in action” Chris Buckett uses this exact same syntax when building a table.

Has something changed or am I misunderstanding something?


This is a browser “feature”. A <tr> element can’t exist on its own and is just dropped from the HTML when it finds one.

The docs to new Element.html() says

Creates an HTML element from a valid fragment of HTML.

Alternatively you can use

      new TableRowElement()..append(new TableCellElement()..text = 'test'));

DartPad example

Answered By – Günter Zöchbauer

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *