How print values in dart to browser

Issue

The below code is to extract parameter from URL using dart
I am able to print the values in console but i am not able to get how to display the output in console to browser that is html version. Please help

import 'dart:core';
import 'dart:html';

void main() {
  var q = '?id=abc&id1=cde&id3=asad';

  if(q.contains('?'))  {

   var a = q.replaceFirst("?"," ");

   var c = a.split("&");
   print(c);
 }

Solution

You can add the output to the body element or you add an element like a <div></div> and insert it inside this element.

To use a ‘div’ element you should add an element in you index.html (or whatever name you have for your HTML page) like

...
<body>
  <div id='someDiv'></div>
</body>

in your Dart code you do it like

import 'dart:core';
import 'dart:html';

void main() {
  var q = '?id=abc&id1=cde&id3=asad';

  if(q.contains('?'))  {

   var a = q.replaceFirst("?"," ");

   var c = a.split("&");
   // querySelector('#someDiv').text = '${c}'; // <= you may need some encoding for special characters like <, >, &, ... 
   var div = querySelector('#someDiv');
   c.forEach((e) => div.append(new DivElement()..text = '${e}'));
 }

Answered By – Günter Zöchbauer

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

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