Get variable from JavaScript into Dart

Issue

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

Thank you.

Solution

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.

Answered By – Zdeslav Vojkovic

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *