Flutter FCM onBackgroundMessage处理程序给出空检查错误



我正在尝试为我的应用程序实现通知,但初始化通知时FirebaseMessaging.onBackgroundMessage(notificationHandler)会出现错误

错误:

E/flutter(28265(:[错误:flutter/lib/ui_dart_state.cc(186(]未处理的异常:在Null值上使用了Null检查运算符E/flutter(28265(:#0 MethodChannelFirebaseMessaging.registerBackgroundMessageHandler(包:firebase_messaging_platform_interface/src/method_channel/messagging.dart:173:53(E/flutter(28265(:#1 FirebaseMessagingPlatform.onBackgroundMessage=(包:firebase_messaging_platform_interface/src/platform_innterface/platform-interface_messaging.dart:108:16(E/flutter(28265(:#2 FirebaseMessaging.onBackgroundMessage(包:firebase_messaging/src/messaging.dart:100:31(

notificationHandler:

Future<void> notificationHandler(RemoteMessage message) async {
/// do sth with data
}

注:FirebaseMessaging.onMessageFirebaseMessaging.onMessageOpenedApp工作时无错误

似乎将FirebaseMessaging.onBackgroundMessage(notificationHandler)notificationHandler函数放在了一个类中。然而,这与FlutterFire的官方文件相矛盾。您需要满足以下条件才能使此功能正常工作:

  1. 它不能是匿名函数
  2. 它必须是顶级函数(例如,不是需要初始化的类方法(

我的建议是,尝试将用作参数的函数notificationHandler放在调用FirebaseMessaging.onBackgroundMessage()的类之外。它对我有效。

最新更新