Dart chrome extension: Listen to chrome api events


To better describe my problem i have created a small example of a chrome extension written in Dart.
You can see the code or download the extension on Gist.

The problem

This example is running fine in Dartium, but when compiled to javascript a typeerror occurs: Uncaught TypeError: undefined is not a function for the line:

context['chrome']['runtime']['onMessage'].callMethod('addListener', [onMessageListener]);

How far i already am

  • As you may see in the example the functions alert() or console.log() via dart:js are also working in the js-extension. So it could be a special problem with dart2js and adding eventlisteners?
  • Also printing out context['chrome']['runtime']['onMessage'] shows the right event-object. (E.g.: context['console'].callMethod('log', [context['chrome']['runtime']['onMessage']]);)
  • I know that there exist an chrome pub package, but there is still a bug when responding to received messages in onMessage. See also this question. Using the chrome api directly via dart:js was the workaround which was fine at that dart version.

I played a lot with the code but all results in the same error. Now i am out of ideas. Hope the community can help me again.

I have now reported this bug on dartbug.com as Robert suggested.
Anyway, I’m still open for a workaround or something if someone know one.


As keerti already mentioned: A similar problem with a workaround can be find here.

My solution looks like this:

  //Tmp: sendResponse is buged, so we use the js-version

  //..and ofcourse the js-version is buged too. So this workaround here:
  var jsOnMessageEvent = context['chrome']['runtime']['onMessage'];
  JsObject dartOnMessageEvent = (jsOnMessageEvent is JsObject ? jsOnMessageEvent : new JsObject.fromBrowserObject(jsOnMessageEvent));
  dartOnMessageEvent.callMethod('addListener', [onMessageListener]);

Answered By – Andi

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

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