来自dart2js检查模式

时间:2016-06-03 08:57:41

标签: dart dart2js

我原来的错误是An attempt was made to use an object that is not, or is no longer, usable。在使用默认设置运行dart2js后,我很难阅读并且无法调试输出,因此我将这些选项添加到pubspec.yaml

- $dart2js:
    checked: true
    sourceMaps: true
    verbose: true
    minify: false

我期待有什么东西可以打破,但我希望错误信息足够明确,我可以解决它,但我理解我得到的错误。

我得到的新错误是:

Error: 
  dart<.wrapException()
  dart<._rootHandleUncaughtError_closure.call$0()
  dart<._microtaskLoop()
  dart<._startMicrotaskLoop<()
  dart<._AsyncRun__initializeScheduleImmediate_internalCallback.call$1<()
  dart<.invokeClosure_closure0.call$0()
  dart<._IsolateContext.eval$1()
  dart<._callInIsolate()
  dart<.invokeClosure<()
  dart<.convertDartClosureToJS/$function</<()

看起来dart在将闭包转换为js函数时遇到了一些麻烦。我认为这将在运行dart2js时完成,而不是在应用程序在浏览器中运行时完成。

我的代码没有任何明确的闭包,但我确实使用async await。我是否正确地假设那些将被转换为封闭?

以防它是相关的,我也使用Angular2。

更新

我的问题似乎是由于我使用HttpRequest.request()中的dart:html造成的。

HttpRequest res = await HttpRequest.request( "${API_URL}/login",
  method      : "POST",
  responseType: "json",
  mimeType    : "application/json",
  sendData    : JSON.encode( req_data ) );

print( res.status );

如果我删除了检查模式并构建,则会给我An attempt was made to use an object that is not, or is no longer, usable错误。

3 个答案:

答案 0 :(得分:2)

API更好,更直观。 例如您的请求:

var client = new BrowserClient();

var response = await client.post("${API_URL}/login", 
                                 body: JSON.encode( req_data ));
var data = JSON.decode(response.body);
client.close();

如果您希望定义请求的方式更精确,可能会在参数中看到。

答案 1 :(得分:1)

我似乎通过更改HttpRequest类的使用来解决问题。

自:

HttpRequest res = await HttpRequest.request( "${API_URL}/login",
  method      : "POST",
  responseType: "json",
  mimeType    : "application/json",
  sendData    : JSON.encode( req_data ) );

print( res.status );

要:

HttpRequest res = new HttpRequest()
  ..open( "POST", "${API_URL}/login" );

req
  ..onLoadEnd.listen( ( ProgressEvent e ) {
      print( res.status );
    })
  ..send( JSON.encode( req_data ) );

我不确定实际问题是什么,但这样我就不会得到InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable或更长的飞镖错误:

Error: 
  dart<.wrapException()
  dart<._rootHandleUncaughtError_closure.call$0()
  dart<._microtaskLoop()
  dart<._startMicrotaskLoop<()
  dart<._AsyncRun__initializeScheduleImmediate_internalCallback.call$1<()
  dart<.invokeClosure_closure0.call$0()
  dart<._IsolateContext.eval$1()
  dart<._callInIsolate()
  dart<.invokeClosure<()
  dart<.convertDartClosureToJS/$function</<()

我的猜测是dart2js在使用async await“隐式”闭包时遇到了一些麻烦,当我使用“显式此处是关闭它的环境”的闭包函数时,它会更加快乐。

感谢GünterZöchbauer的帮助。

答案 2 :(得分:0)