Dart SDK 0.8.10.3_r29803 dart:js callbacks

久未见 提交于 2019-12-10 10:35:25

问题


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...


回答1:


Just pass your Dart function into JavaScript and it'll automatically converted to a JavaScript function.

Dart:

import 'dart:js';

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

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

JS:

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

For data passed to the Dart callback, many types will be automatically converted (see the list here: http://api.dartlang.org/docs/releases/latest/dart_js.html) and other types will give you a JsObject proxy.



来源:https://stackoverflow.com/questions/19800265/dart-sdk-0-8-10-3-r29803-dartjs-callbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!