1

I'm trying to convert my project to dart step by step. I have converted a stand alone library to Dart and I'm using the Dart compiled javascript in my project. Thanks for the help in my other question. I'm able to call Dart compiled javascript functions. Now I facing another puzzle. If my Dart function requires a callback, how do I passing a Javascript function into the Dart generated javascript function?

What I am trying to do is register a Dart event handler from Javascript, for example:

in my Dart, I have event bus, dart object can register event handler through:

bus.on('eventName', callbackFunc);

and Dart object fires a event through:

bus.fire('eventName', data);

I expose the bus to Javascript world, through:

js.context['registerEvent'] = bus.on;

In Javascript, I want to call

registerEvent('someEvent', function() { console.log('JS callback' });

to register an event handler, when Dart object fired an event, the JS callback will be invoked.

2 Answers 2

2

Can you try to just pass it as a parameter to Dart and call it with param.apply([])

Sign up to request clarification or add additional context in comments.

2 Comments

param.apply() doesn't work. Uncaught NoSuchMethodError: Cannot call "apply$0" on "#<JsFunction>" (Object #<JsFunction> has no method 'apply$0')
Try param.apply([])
1

from your previous question,

js.context["sayGreeting"] = (message) {
  print("greeting: $message");
};

// pass js function which is already defined above.
someJsObject.callMethod('someOriginalJsFunction', [js.context['sayGreeting']]);

1 Comment

I added more information in my question. Your method won't work because 1. dart didn't know the callback function name. 2. there might not be a function name at all, like the inline function in the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.