1

I know how Dart to use other javascript through dart.js library. How do I use Dart generated JavaScript in other JavaScript? How do I call a function in Dart generated javascript?

1 Answer 1

2

Using JS interop, you can call Javascript from Dart, and Dart from Javascript.

To call Dart from Javascript, you first need to register the functions that you want Javascript to have access to. It's also possible to pass primitives between JS and Dart and vice-versa.

import 'dart:html';
import 'dart:js' as js;

void main() {
  js.context["myFunc"] = () {
    print("called from javascript");
  };

  js.context["sayGreeting"] = (message) {
    print("greeting: $message");
  };
}

Then from Javascript, you can call the functions that you registered:

myFunc(); // prints 'called from javascript'
sayGreeting("Hello"); // prints 'greeting: Hello'
Sign up to request clarification or add additional context in comments.

1 Comment

Exporting dart function in Javascript worked, but the exposed function might conflict with a function from other library (have same name). It is possible to warp those exported functions into a Javascript object? So in Javascript I can call myClass.myFunc().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.