How to define a method for the class 'Proxy' in Dart js-interop?

Issue

I’m currently calling a jQuery based plugin called Bootstrap Context Menu.

In order to call it, I need to use the Javascript Interop library. But when I call a jQuery method from it I receive the following warning:

The method 'jQuery' is not defined for the class 'Proxy'

Code snippet:

  js.scoped(() {
    js.context.jQuery('#canvas').contextmenu();
  });

This was not happening before some dart/js-interop updates. What is the right way to get rid of this warning?

Solution

You get this warning because the new analyzer doesn’t seem to be aware of the option Report ‘no such member’ warnings when class defines noSuchMethod() ( Reported at http://dartbug.com/10016 ). If you switch back to the legacy analyzer you shouldn’t see this warning anymore.

That said if you want to use the new analyzer and get rid of this warning you can use the array notation like this :

js.context["jQuery"]('#canvas')["contextmenu"]();

But :

  • it’s less readable particullary for method calls.
  • it’s less efficient for method calls because 2 operations are done ( f = js.context["jQuery"] followed by f('#canvas') ) instead of 1 ( js.context.jQuery('#canvas') )

Answered By – Alexandre Ardhuin

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

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