Uncaught TypeError "is not a function" in js interop callback function

Issue

I have created this sample project: https://github.com/mfMeds/jsdart
to reproduce my error.

The error:

main.dart.js:5036 Uncaught TypeError: J.ab(...).ge5 is not a function
    at Object.J.fm (main.dart.js:5036)
    at cH.dart.cH.eu (main.dart.js:4962)
    at Object.eval (eval at di (main.dart.js:786), <anonymous>:3:36)
    at Object.ir (main.dart.js:628)
    at dart.kZ (main.dart.js:4005)
    at ChartElement.<anonymous> (main.dart.js:3998)
    at ChartElement.update (Chart.js:8803)
    at ChartElement.handleEvent (Chart.js:9100)
    at Chart.eventHandler (Chart.js:4521)
    at listener (Chart.js:4455)

Only when I am using an attribute of the variable a I am getting this error. With window.console.log(a) it is logging the object without error.

My error happens here: https://github.com/mfMeds/jsdart/blob/master/lib/src/zgraph/zgraph.dart#L80

@override
ngAfterViewInit() {
  List<String> myDays = new List();
  List<double> myData = new List();

  myDays.add('Monday');
  myData.add(12.0);

  var data = new LinearChartData(labels: myDays, datasets: <ChartDataSets>[
    new ChartDataSets(label: 'My Label', steppedLine: true, data: myData),
  ]);

  var aOptions = new ChartAnimationOptions();

  var config = new ChartConfiguration(
      type: 'bar',
      data: data,
      options: new ChartOptions(
          responsive: true,
          maintainAspectRatio: false,
          legend: new ChartLegendOptions(display: false),
          //scales: new ChartScales(yAxes: [new ChartYAxe(ticks: new TickOptions(max:20))]),
          animation: aOptions,
          tooltips: new ChartTooltipOptions(
            enabled: false,
            custom: allowInterop(customTooltip), // <-- binding the method here
          )));

  new Chart(mycanvas, config);
}

void customTooltip(dynamic a) {
  window.console.log(a);
  if (a.opacity == 0) { // <-- The error happens here
    //tooltipEl.style.opacity = '0';
    return;
  }
}

When I am serving the application like that everything works:

pub global run webdev serve web:xxxx. 

Only when building the application like that I get this error:

pub global run webdev build --output=web:build

Solution

In the docs they write:
The properties of the JavaScript object are accessible via the [] and []= operators. Methods are callable via callMethod.

I tried this also, but it does not work…

I found a other solution. dynamic a is a js object. You can access js objects with js.getProperty I made a class for this:

import 'package:js/js_util.dart' as js;


class JsObject {
  final dynamic _object;

  const JsObject(this._object);

  dynamic operator[](String name) => js.getProperty(_object, name);
  operator[]=(String name, dynamic value) => js.setProperty(_object, name, value);
}

Then you can access with

// a is your js object
var jsA = new JsObject(a);
jsA['YOUR_PROPERTY']

Answered By – GeroTreeman

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

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