How to pass an additional variable to a callback function in Dart

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

+4
source share
1 answer

listen(callbackFunction)Use instead 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
}
+5
source

All Articles