0

I have a bunch of Dart libraries that a customer wants to call from JavaScript. Is it possible to call functions created with dartdevc from JS?

jfp
  • 381
  • 1
  • 3
  • 8
  • 2
    Sure, but DDC output is not intended to be used in production, it's only for development and for production still dart2js should be used. I have heard, that some actually use DDC for production, but it's not officially supported. `dart2js` output can't be called from JS except specific predefined entry-points. – Günter Zöchbauer Aug 02 '17 at 15:44
  • Related: https://stackoverflow.com/questions/45245957/compiling-dart-into-minifier-friendly-javascript-from-dartdevc-into-google-clos/45247781#45247781 – matanlurey Aug 02 '17 at 22:35

1 Answers1

0

If you only want to call functions, you can probably do that in this way. Although I only know it's works for functions, probably not for whole class. I am also not sure about dartdevc support, but dart2js should be able to do this:

import 'dart:js' as js;

main() {
  js.context['methodFromDart'] = doMyStuff;
}

void doMyStuff(String text) => print(text);

And then in you Javascript you are free to do:

methodFromDart("Hello world to Dart!");
tenhobi
  • 1,977
  • 3
  • 23
  • 49