4

I have a JavaScript library being imported in my HTML Documents head. How can I access objects from this library?

Thank you.

1 Answer 1

5

The interop with JavaScript is desribed in article 'Using JavaScript from Dart: The js Library'

In short, you have to:

//import the JS interop lib
import 'package:js/js.dart' as js;

// access the JS context for the page
var context = js.context;

// then use context to access JS object
var canvas = query('#map_canvas');
var googlemaps = js.context.google.maps;

// and create JS objects accessed through proxies
var googlemap = new js.Proxy(googlemaps.Map, canvas);

For more details (scopes, lifetimes, callbacks, etc) and examples, refer to linked article.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a ton! By any chance, would you know what the type is for js or context off the top of your head? I like to type strongly where I can.
js is just an alias/namespace for the lib - it is not an object, and it is equal to whatever you put in as js part. you can also omit it and use context as a top level identifier. The type of context is Proxy (see dart-lang.github.io/js-interop/docs/js.html#context and github.com/dart-lang/js-interop/blob/master/lib/js.dart). For more info, see

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.