Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import dart:html & dart:io in the same class?

Tags:

dart

The code below "looks right", it compiles, but does not run, failing with the console message:

Cannot load Dart script dart:io
Failed to load resource

If I comment out the #import('dart:io');, wrong I believe, I get a compilation error, but it launches and not until I press the button, do I get the runtime error:

Internal error: 'http://127.0.0.1:3030/home/david/dart/samples/htmlIO/htmlIO.dart': Error: line 13 pos 26: type 'HttpClient' is not loaded
var connection = new HttpClient().get('www.google.com', 80, '/');

... which is expected.

So my question is: How do I import dart:html & dart:io in the same class?

#import('dart:html');
#import('dart:io');

class htmlIO {

  ButtonElement _aButton;

  htmlIO() {
  }

  void handlePress(Event e) {
    var connection = new HttpClient().get('www.google.com', 80, '/');
    write('made it');
  }

  void run() {
    _aButton = document.query("#aButton");
    _aButton.on.click.add(handlePress);
    write("Hello World!");
  }

  void write(String message) {
    // the HTML library defines a global "document" variable
    document.query('#status').innerHTML = message;
  }
}

void main() {
  new htmlIO().run();
}
like image 495
user1334731 Avatar asked Apr 15 '12 17:04

user1334731


People also ask

Can browser Read dart?

Although no production browsers can execute Dart code directly, all modern browsers can execute Dart code that's been compiled to JavaScript.

What is HTML pub?

Digital Publishing Platform & Software for Magazines,Catalogs,Brochures,FlipBook & More.


1 Answers

dart:html is a client side library, whereas dart:io is a server side library. dart:html makes use of features of the browser, but dart:io makes use of features that are restricted by browser security (such as file system access an so-on).

It may be that the time comes that you can use dart:html on the server, with a "mocked" browser, which could be useful for unit testing and the like, but you can't do that yet.

like image 199
Chris Buckett Avatar answered Nov 20 '22 00:11

Chris Buckett