参数类型'Future<dynamic>? Function(String?)' 无法分配给参数类型 'DidReceiveNotificationResponseCallback?'

问题描述 投票:0回答:1

尝试将 onSelectNotification 更新为 onDidReceiveNotificationResponse 时出现错误

  flutterLocalNotificationsPlugin.initialize(initSettings,
      onDidReceiveNotificationResponse: _onSelectNotification);
Future<dynamic>? _onSelectNotification(String? payloadNotification) {
  if (kDebugMode) {
    print("Notification selected.");
  }

  WidgetsBinding.instance.addPostFrameCallback((_) {
    if (payloadNotification != null && payloadNotification != "") {
      if (kDebugMode) {
        print(payloadNotification);
      }

      // parse notification content like this.
      // PushRequest.Message message =
      // PushRequest.Message.fromJson(jsonDecode(payloadNotification));
      //
    }
  });
  return null;
}

将其重命名为新错误时,它只是抛出此错误“参数类型“Future?Function(String?)”无法分配给参数类型“DidReceiveNotificationResponseCallback?”。”

flutter flutter-local-notification
1个回答
0
投票

onDidReceiveNotificationResponse
必须是
DidReceiveNotificationResponseCallback
类型,而您的
_onSelectNotification
不是。

DidReceiveNotificationResponseCallback
定义为:

typedef DidReceiveNotificationResponseCallback = void Function(
    NotificationResponse details);

所以你需要改变

Future<dynamic>? _onSelectNotification(String? payloadNotification) {

void _onSelectNotification(NotificationResponse payloadNotification) {

也许可以将其设为

Future
而不是
void
,但我对此并不完全确定。

© www.soinside.com 2019 - 2024. All rights reserved.