When calling dart compiled Javascript from javascript how do I pass in a callback function into dart compiled Javascript?

StackOverflow https://stackoverflow.com/questions/22726567

  •  23-06-2023
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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']]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top