如何在Dart中将额外变量传递给回调函数

Lev*_*ara 4 dart

我需要在回调函数中从main访问变量.回调函数只有一个参数Event.除了将其设置为全局变量之外,从回调访问变量的首选方法是什么?是否可以将其作为额外参数传递给回调?

mer*_*ert 6

而不是listen(callbackFunction),使用listen((SomeEvent e) => callbackFunction(e, myOtherParameter));.

例如,

document.querySelector("div#someElement")
    .onClick.listen((MouseEvent e) => callbackFunction(e, myOtherParameter))
Run Code Online (Sandbox Code Playgroud)

将调用以下函数

void callbackFunction(MouseEvent e, myOtherParameter) {
   // Do something with your parameter
}
Run Code Online (Sandbox Code Playgroud)