Flutter Local Notification onSelectNotification 函数参数类型无法分配

Posted

技术标签:

【中文标题】Flutter Local Notification onSelectNotification 函数参数类型无法分配【英文标题】:Flutter Local Notification onSelectNotification function argument type can't be assinged 【发布时间】:2021-07-20 15:02:06 【问题描述】:

我正在为我的应用程序使用Local Notifications,我正在尝试设置onSelectNotification,但我无法使其工作。我就是这样init:

 Future initialize() async 
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();

    androidInitializationSettings androidInitializationSettings =
        AndroidInitializationSettings('app_icon');

    iosInitializationSettings iosInitializationSettings =
        IOSInitializationSettings();

    final InitializationSettings initializationSettings =
        InitializationSettings(
            android: androidInitializationSettings,
            iOS: iosInitializationSettings);

    await flutterLocalNotificationsPlugin.initialize(
      initializationSettings, onSelectNotification: onSelectNotification
    );
  

还有我的onSelectNotification

  Future onSelectNotification(String payload) async 
    print(payload);
  

但 Flutter 在抱怨:

参数类型“Future Function(String)”不能分配给参数类型“Future Function(String?)?”。

我在文档中找不到任何关于此的内容,在其他任何地方都找不到。我想念她什么?

【问题讨论】:

【参考方案1】:

应该是

Future<void> onSelectNotification(String? payload) async ...

而不是

Future<void> onSelectNotification(String payload) async ...

String payload 上的 ? 不见了

【讨论】:

【参考方案2】:

改为使用 onSelectNotification to Future dynamic >。

Future<dynamic> onSelectNotification(payload) async 
   // implement logic
   print("onSelectNotification : " + payload);

【讨论】:

以上是关于Flutter Local Notification onSelectNotification 函数参数类型无法分配的主要内容,如果未能解决你的问题,请参考以下文章

Flutter发送系统通知(flutter_local_notifications)

Flutter发送系统通知(flutter_local_notifications)

Flutter发送系统通知(flutter_local_notifications)

Flutter发送系统通知(flutter_local_notifications)

Flutter:在前台运行时,flutter_local_notifications 不起作用

Flutter 本地通知正文未显示所有通知文本(flutter_local_notifications 包)