Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass extra variable to callback function in Dart

Tags:

dart

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?

like image 645
Levara Avatar asked Dec 16 '13 01:12

Levara


1 Answers

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
}
like image 187
mert Avatar answered Sep 20 '22 02:09

mert