You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter报错:未找到SelectNotificationCallback类型,求修复

修复Flutter本地通知插件SelectNotificationCallback未找到的错误

1. 补充回调类型定义

在你的FlutterLocalNotificationsPlugin类所在的dart文件最顶部,添加以下类型定义:

typedef SelectNotificationCallback = Future<void> Function(String? payload);

这个typedef对应通知点击回调的标准签名,参数是通知携带的payload(可为空字符串),返回值为Future<void>。

2. 修正onSelectNotification参数的类型声明

找到FlutterLocalNotificationsPlugin类中onSelectNotification的声明位置,确保它的类型是SelectNotificationCallback?,示例如下:

class FlutterLocalNotificationsPlugin {
  // 其他成员变量...
  final SelectNotificationCallback? onSelectNotification;

  FlutterLocalNotificationsPlugin({
    this.onSelectNotification,
    // 其他构造参数...
  });

  // 其他方法...
}

如果原来的代码里这个参数的类型写的是其他内容,直接替换成SelectNotificationCallback?即可。

3. 检查回调调用的兼容性

如果类内部有调用onSelectNotification的逻辑,确保调用方式符合类型定义,比如:

// 处理通知点击的示例代码
void _handleNotificationTap(String? payload) async {
  if (onSelectNotification != null) {
    await onSelectNotification!(payload);
  }
}

保证传递的payload参数是String?类型,和typedef的要求一致。

问题原因

你遇到的错误是因为旧版本插件代码依赖的SelectNotificationCallback类型在后续的插件更新中被移除,或者你的插件代码本身缺失了这个类型定义。手动补上这个typedef就能解决类型未找到的问题。如果后续还有其他API报错,建议对照当前flutter_local_notifications的API规范,同步调整其他过时的代码逻辑。

内容的提问来源于stack exchange,提问作者Kevin Damerr Tan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 17:45:48