如何在同一个类中导入dart:html&dart:io?

下面的代码“看起来正确”,它编译,但不运行,失败的控制台消息:

Cannot load Dart script dart:io
Failed to load resource

如果我注释掉#import(‘dart:io’);我错了,我得到一个编译错误,但它启动,直到我按下按钮,我是否收到运行时错误

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,‘/’);

……这是预期的.

所以我的问题是:如何导入dart:html&飞镖:io在同一个班级?

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

class htmlIO {

  ButtonElement _aButton;

  htmlIO() {
  }

  void handlePress(Event e) {
    var connection = new HttpClient().get('www.google.com','/');
    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();
}

解决方法

dart:html是客户端库,而dart:io是服务器端库. dart:html利用了浏览器的功能,但dart:io利用了受浏览器安全性限制的功能(例如文件系统访问等).

可能是时候你可以在服务器上使用dart:html,使用“模拟”浏览器,这可能对单元测试等有用,但你还不能这样做.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 <div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些