Is there a way to catch a JavaScript exception in Dart?


I have a <core-icon> element like

<polymer-element name="app-element">
    <core-icon icon="{{icon}}"></core-icon>
  <script ...>
class AppElement extends PolymerElement {
  AppElement.created() : super.created();

  String icon = 'menu';

  clickHandler(e) {
    icon = null;

This leads to this exception

Exception caught during observer callback: TypeError: Cannot read property 'split' of null
    at core-icon.Polymer.updateAlt (http://localhost:63342/core-elements/packages/core_elements/src/core-icon/core-icon.html:188:50)
    at core-icon.Polymer.updateIcon (http://localhost:63342/core-elements/packages/core_elements/src/core-icon/core-icon.html:147:14)
    at core-icon.g.invokeMethod (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:13:25932)
    at core-icon.g.notifyPropertyChanges (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:13:24037)
    at Object.x.report_ (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:12:18274)
    at Object.S.check_ (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:12:22612)
    at c (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:12:12181) polymer.concat.js:4861x.report_ polymer.concat.js:4861S.check_ polymer.concat.js:5264c

In my opinion this is a but in <core-icon> and I’m going to create an issue but my question is anyway:

Is there a way to catch such a JavaScript exception in Dart?

I of course tried to wrap the line icon = null; with a try/catch or window.onError.listen((e)...); without success though.


I haven’t actually tried it but I guess it is the solution:


It looks like the actual issue is the exception is being caught on the js side and then just printed to the console, so that is why we can’t get access to it. They do set Observer._errorThrownDuringCallback to true though, so you can add the following in your test and it will fail appropriately:

expect(context['Observer']['_errorThrownDuringCallback'], false,
    reason: 'Setting icon to null shouldn\'t throw an error.');

Answered By – Günter Zöchbauer

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

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