微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在同一个类中导入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,使用“模拟”浏览器,这可能对单元测试等有用,但你还不能这样做.

原文地址:https://www.jb51.cc/html/226117.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐