How to call a Dart function from JavaScript

You should try Dart's JS interop library.

You can define a callback function inside Dart and export it (in a way) to JavaScript.

Take a look at this example:

Dart code defines JS function

 context['javascriptFunctionName'] = (parameter) {
       //call any Dart method
    }

then call it from JavaScript:

javascriptFunctionName({'param': 'value'});

With package js instead of dart:js it can be made available to JS this way:

import 'dart:html';
import 'package:js/js_util.dart';

void main() {
  setProperty(window, 'callDartFunc', allowInterop(dartFunc));
}

String dartFunc() => 'Hello World';

Thanks to Matan Lurey https://gitter.im/dart-lang/TALK-general?at=585b9b42db9cafe9183a3345

Tags:

Dart