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

Issue

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.

Solution

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

Answered By – Günter Zöchbauer

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *