Search code examples
callbackdartdart-js-interop

Closure call with mismatched arguments: function 'call'


I'm using the (2.0)js-interop library in combination with the JS library ImageLoaded and I'm stuck the FunctionProxy class because the code below throw the following error:

Breaking on exception: Closure call with mismatched arguments: function 'call'

js.FunctionProxy loaded = new js.FunctionProxy((){
      print("called");
      js.Proxy pckry = new js.Proxy(context.Packery, container, options);
    });
    
js.Proxy img = new js.Proxy(context.imagesLoaded, container, loaded);

Which is weird because my js callback is called 5 times before the app crashes.


Solution

  • Looking at the Usage section of imagesLoaded it looks like the callback takes one parameter. So you have to add this parameter to your callback :

    js.FunctionProxy loaded = new js.FunctionProxy((instance) {
      print("called");
      js.Proxy pckry = new js.Proxy(context.Packery, container, options);
    });
    
    js.Proxy img = new js.Proxy(context.imagesLoaded, container, loaded);
    

    Additional notes :

    • You can avoid new js.FunctionProxy. There are only a limited number of cases where it's needed and your case here is not one of them.
    • imagesLoaded can be use as a function and it simplifies the code.

    Thus, you should be able to use :

    final img = context.imagesLoaded(container, (instance) {
      print("called");
      js.Proxy pckry = new js.Proxy(context.Packery, container, options);
    });