【发布时间】:2023-04-10 19:48:01
【问题描述】:
我原来的错误是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。
更新
看来我的问题是由我使用来自dart:html 的HttpRequest.request() 引起的。
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 错误。
【问题讨论】:
-
您可以尝试
pub build --mode=debug禁用摇树和缩小。用于将 dart 脚本添加到index.html的脚本标签看起来如何?可能是一些时间问题。 -
我会给
--mode=debug一个机会,让你知道我得到了什么。我正在使用这些脚本标签:<script async src="main.dart" type="application/dart"></script><script async src="packages/browser/dart.js"></script> -
你能把项目发布到 GitHub 仓库吗?
-
据我所知,只是
HttpRequest.request()函数导致了问题。我要把它改成new HttpRequest()..open(...)看看是否有帮助。如果没有,我会尝试将 github 存储库放在一起。 -
如果只是这个调用,你可以试试看能否重现dartpad.dartlang.org中的错误