2

I have a Dart class (foo.dart):

class Foo {
  void talk() {
    print('Hello');
  }
}

After compiling foo.dart to JavaScript, I'd like to be able to use Foo like this:

var foo = new Foo(); // from foo.dart.js
foo.talk() // prints "Hello"

My questions:

  • Is this currently possible?
  • If so, how?
  • If not, what plans, if any, are in place to make it possible?

The dart:js library documentation states:

This library does not yet make Dart objects usable from JavaScript, their methods and proeprties [sic] are not accessible, though it does allow Dart functions to be passed into and called from JavaScript.

That word "yet" offers some hope, but I've found very little on this topic anywhere else.

Edit:

I do realize it's possible to call Dart functions from JavaScript using dart2js. However, what I'm trying to do is somewhat different. I'd like to be able to access all of the functionality of a Dart class from JavaScript.

2

1 Answer 1

2

Due to tree-shaking and minification this is normally not possible. If you have a Dart application (with a main() then you can make a Dart function available to be called from JavaScript (see How to call a Dart function from Javascript? for an example).

As far as I know there are plans to support your requirement but I have no idea about progress or when such a feature might be available.

This is the related project https://github.com/dart-lang/js-interop

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

4 Comments

The js-interop package looks like it requires a Dart vm. Is this true? I ask because the documentation test .html file uses Dart script github.com/dart-lang/…
You have to run pub build in the project directory to build it to JavaScript.
Hey Günter, do you know if there is any update on this?
@Tobika nothing really changed

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.