2

Can some one please give me an example of the Dart code that would follow this flow

1) Dart call Javascript function 2) Javascript loads some data via Javascript api's 3) Javascript returns data to Dart

Currently I can only call the Javascript function I need (via js.context.callMethod('myAmazingFunction');) but I can't receive the callback. I thought there would be something like js.context.listenForMethod('myAmazingCallback'); or similar...

4

1 回答 1

4

只需将 Dart 函数传递给 JavaScript,它就会自动转换为 JavaScript 函数。

镖:

import 'dart:js';

myCallback(data) {
  print('received $data');
}

main() {
  context.callMethod('mJsFunction', [myCallback]);
}

JS:

function myJsFunction(callback) {
  callback('some data');
}

对于传递给 Dart 回调的数据,许多类型将被自动转换(参见此处的列表:http: //api.dartlang.org/docs/releases/latest/dart_js.html),其他类型将为您提供 JsObject 代理。

于 2013-11-06T02:59:48.963 回答