尝试将 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?”。”
onDidReceiveNotificationResponse
必须是 DidReceiveNotificationResponseCallback
类型,而您的 _onSelectNotification
不是。
DidReceiveNotificationResponseCallback
定义为:
typedef DidReceiveNotificationResponseCallback = void Function(
NotificationResponse details);
所以你需要改变
Future<dynamic>? _onSelectNotification(String? payloadNotification) {
到
void _onSelectNotification(NotificationResponse payloadNotification) {
也许可以将其设为
Future
而不是void
,但我对此并不完全确定。