Edit: Here is a minimal project that illustrates my issue. You can see the error described by serving it to the browser: pub get
and then either pub serve
(dartium) or pub build --mode=debug
(other browsers).
How can I access an arbitrary JavaScript property from Dart through a JsObjectImpl? I am using the ace.js library with an interop to Dart that I've adapted from a typescript interface, and the method I am calling returns a plain javascript object with key-value pairs.
Dart gives me a JsObjectImpl, which cannot be casted to a Map or a JsObject, both of which have [] accessors. It confusingly seems to inherit from the deprecated JSObject (note the 's' is capitalized in the latter) which does not have the [] accessor, so I can't get the data out.
Some error messages:
When attempting a cast from JsObjectImpl to JsObject:
ORIGINAL EXCEPTION: type 'JSObjectImpl' is not a subtype of type 'JsObject' of 'obj' where JSObjectImpl is from dart:js JsObject is from dart:js
. I get a similar message when usingMap
as well.Looking at the object in the debugger, I can frustratingly see the property in JS view but not in the Dart object:
The4: Object
is the data I want.
Ok, this was a fun one, happy holidays :)
It looks like
Map
is not a supported auto-conversion forpackage:js
. So a couple of things:For interested parties, we can use the browser-native
Object.keys
:And call it once we have an arbitrary JavaScript object: