How to access bound this in js-interop?

Issue

I am trying to use React library from withing Dart.

However, I need to provide various functions that are called by React with this bound to some javascript object. I know, that I can provide Dart callbacks to javascript code using js interop Callback class, however I have no idea, how to access javascript this within these callbacks.

Could you please help me find out?

Solution

You have to use the optional named parameter withThis on Callback creation. With that, the javascript this will be added as first parameter when the dart function will be called. Something like :

var func = (jsThis, arg1) {
  // do the job with jsThis
};
new Callback.many(func, withThis: true);

Answered By – Alexandre Ardhuin

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.