How to call JS function with new js package (0.6.0)

index.html

<!doctype html>
<html>
  <head>
    <script>
      var testFunction = function() {return 'xxx'};
    </script>
  </head>
  <body>
    <script type="application/dart" src="index.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>

index.dart

import 'dart:js' as js;
import 'package:js/js.dart';

@Js() // about to being changed to @JS
external String testFunction();

main() {
  // fails: Exception: Uncaught Error: No top-level method 'testFunction' declared.
  print(testFunction()); 

  // works
  print((js.context['testFunction'] as js.JsFunction).apply([]));
}

Dart VM version: 1.13.0-edge.a598fea28cf26ed82b0a197e65af33a7edca5cac (Thu 15 Oct 18:02:15 2015) on "linux_x64"

+4
source share
1 answer

Works as expected.

I just had Dartium working during the Dart update, and I still had the old version when I tried this example. After closing and reopening Dartium, the code worked fine.

+2
source

Source: https://habr.com/ru/post/1611902/


All Articles