dart2js js-interop crashes when trying to call function

Consider the following example:

main.dart:

import 'dart:js';

void main() {
  var m = allowInterop((x) => x + 1);
  context.callMethod('test', [m]);
}

index.html:

<script>
  window.test = function(m) {
    console.log(m(1));
  }
</script>

<script src="main.dart.js"></script>

I expect this to print “2” to the console, instead I get this:

main.dart.js:502 Uncaught NoSuchMethodError: method not found: 'call'
Receiver: Instance of 'JavaScriptFunction'
Arguments: [1]
    at Object.wrapException (file:///usr/local/google/home/het/Projects/dart2js_example/main.dart.js:502:17)
    at JavaScriptObject.noSuchMethod$1 (file:///usr/local/google/home/het/Projects/dart2js_example/main.dart.js:2399:15)
    at Object.noSuchMethod$1$ (file:///usr/local/google/home/het/Projects/dart2js_example/main.dart.js:1814:42)
    at Object.Primitives_functionNoSuchMethod (file:///usr/local/google/home/het/Projects/dart2js_example/main.dart.js:382:16)
    at Object.Primitives__genericApplyFunction2 (file:///usr/local/google/home/het/Projects/dart2js_example/main.dart.js:441:18)
    at Object.Primitives_applyFunction (file:///usr/local/google/home/het/Projects/dart2js_example/main.dart.js:416:16)
    at Object.Function_apply (file:///usr/local/google/home/het/Projects/dart2js_example/main.dart.js:2022:16)
    at _callDartFunction (file:///usr/local/google/home/het/Projects/dart2js_example/main.dart.js:2224:31)
    at file:///usr/local/google/home/het/Projects/dart2js_example/main.dart.js:3589:18
    at window.test (file:///usr/local/google/home/het/Projects/dart2js_example/index.html:3:17)

I can’t use package:js because this is for code that lives in a dart: library.

Author: Fantashit

1 thought on “dart2js js-interop crashes when trying to call function

Comments are closed.