How to call a JavaScript function named `call` from dart

Issue

Is there any way to call a JavaScript function named call() (in a nested object) from Dart or do I have to wait for Dart 2.0 from which the special handling of call() might get removed?

I have a JS Proxy like:

@JS()
class SomethingFancy {
  external String call();
}

But as call() can be used to turn an object into a function, it makes it impossible to access the function of the JS object.

If I could, I would change the name of the method in Dart, but that’s not supported by package:js:

/// By default the dart name is used. It is not valid to specify a custom     
/// [name] for class instance members.

The error I get is:

Uncaught Error: NoSuchMethodError: method not found: 'call$0' (J.getSomethingFancy$1$x(...).call$0 is not a function)

If the function didn’t exist, the error would look like this:

Uncaught Error: NoSuchMethodError: method not found: 'callMe' (receiver.callMe is not a function)

Other functions on the same object work just fine.

Solution

You can prefix call with JS$:

@JS()
class SomethingFancy {
  external String JS$call();
}

JS$ can be used as prefix to allow to access to JS names that conflicts with dart keywords.

Answered By – Alexandre Ardhuin

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

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