我是个飞镖新手,所以才会飘动。我就是这样使用NotificationListener的
String x = a; // or could be x=b, x=c, etc, comes as function parameter
return NotificationListener(
onNotification: onNotificationHandler,
child: new ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
physics: const BouncingScrollPhysics(),
// other codes comes here
问题是,
我希望onNotification
的值在变量x
的值的基础上是动态的,有人能帮我吗?
发布于 2019-11-10 22:01:15
在Dart中,Function
是一等公民,这意味着函数可以从方法返回。
然后,您可以创建一个方法,该方法以x
作为参数,并返回一个NotificationListenerCallback
类型的函数。返回的函数是所谓的Closure
,这意味着它可以访问它的词法作用域(本例中为x
)中的变量,即使它在外部执行也是如此。
在您的示例中,它可以是:
String x = a; // or could be x=b, x=c, etc, comes as function parameter
return NotificationListener(
onNotification: _notificationHandler(x), // The return the appropriate function with x scoped to the function to be used later
child: new ListView.builder(
// other codes comes here
def _notificationHandler(String value) => (T notification) {
// Note that the returned function has access to `value`
// even if it will be executed elsewhere
return (value == 'Hey);
}
https://stackoverflow.com/questions/58789207
复制相似问题