on method

void on(
  1. String type,
  2. void listener(
    1. Event
    ), [
  3. bool? useCapture
])

Adds an event listener to this target with automatic Dart-to-JS callback conversion.

This is a convenience method that wraps listener using jsCallback and calls addEventListener.

element.on('click', (e) {
  print('Clicked!');
});

Implementation

void on(String type, void Function(web.Event) listener, [bool? useCapture]) {
  // On the server, stubs.dart's EventTarget.addEventListener accepts dynamic callback
  // On the browser, we need to convert the Dart function to a JS callback
  final callback = js_callback.jsCallbackImpl(listener);

  // Use the helper to handle platform-specific type conversion (e.g. .toJS)
  js_callback.addEventListener(this, type, callback, useCapture);
}