How to pass extra variable to callback function in Dart

dart

Solution

Rather than `listen(callbackFunction)`, use `listen((SomeEvent e) => callbackFunction(e, myOtherParameter));`.

For example,

document.querySelector("div#someElement")
    .onClick.listen((MouseEvent e) => callbackFunction(e, myOtherParameter))

will call the following function

void callbackFunction(MouseEvent e, myOtherParameter) {
   // Do something with your parameter
}

Problem

I need to access a variable from main in a callback function. Callback functions only have one parameter, Event. What would be preferred way to access a variable from callback, other than setting it as global variable? Is it possible to pass it to callback as an extra parameter?

Original source