Dart JS互操作-将JS上下文作为参数传递

问题描述

如何将上下文从Dart传递到JavaScript?

我正在尝试:

JS函数

function foo(this,(arg) {
    console.log(arg);
});

Dart

@JS("LibJS")
library libjs;

import 'package:js/js.dart';

@JS("foo")
external dynamic foo(dynamic context,dynamic function);

void main() {
  var obj = foo(context,(arg) {});
}

解决方法

@JS() // if your JS call isn't LibJS.foo() then remove the string
library libjs;

import 'package:js/js.dart';

@JS() // no need to add the "foo" if the name of the function is foo
external dynamic foo(dynamic context,Function() function);

void main() {
  var obj = foo(context,allowInterop((arg) {
     return 'hello';
  }));
}