flutter - Flutter:通知监听器,如何让onNotification动态化
问题描述
我是飞镖新手,因此会颤抖。我正在使用这样的 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
有人可以帮助我吗?
解决方案
在 Dart 中,Function
是一等公民,这意味着一个函数可以从一个方法返回。
然后,您可以创建一个方法,该方法将其x
作为参数并返回一个NotificationListenerCallback
. 返回的函数就是所谓的 a Closure
,这意味着x
即使它在外部执行,它也可以访问其词法范围内的变量(在这种情况下是 is )。
在您的示例中,它可以是:
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);
}
推荐阅读
- c# - 如何使用 Restsharp 在 c# 中使用 flickr api 发布?
- tags - Nuxtjs 元在 vercel 部署中不起作用。但在本地开发服务器上工作
- amazon-web-services - DynamoDB 日期 GSI
- node.js - 如何在 Node.js 中使用 Express 服务器渲染 React 应用程序?
- algorithm - 将随机范围从 0-2 缩小到 0-1
- java - 处理表格时的硒问题
- lumen - 我们如何为 Lumen Passport 设置令牌到期
- nuxt.js - 如何使用发布数据重定向到 Nuxt.js 中的外部 URI?
- c# - 我不知道如何通过 Windows 窗体应用程序在 mySQL 表中正确分组我的串行数据
- excel - Excel VBA 数字格式选项如何适用于具有不同区域设置的用户?