未处理异常:空值(颤振FCM)上使用的空检查操作符



我正在尝试为我的应用程序实现通知,但是当初始化通知时FirebaseMessaging.onBackgroundMessage((message) =>myBackgroundMessageHandler(message))给出以下错误。我已经查看了这个问题和这个问题,并采取了函数myBackgroundMessageHandler,我正在类外使用作为参数,但错误仍然存在。

下面是错误:

E/flutter (10115): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)]未处理异常:对空值使用空检查操作符E/flutter (10115): #0
methodchannelfirebasemessagage . registerbackgroundmessagehandler(包:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart: 179:53)E/flutter (10115): #1
FirebaseMessagingPlatform.onBackgroundMessage=(包:firebase_messaging_platform_interface/src/platform_interface/platform_interface_messaging.dart: 101:16)E/flutter (10115): #2 firebasemessagingonbackgroundmessage(包:firebase_messaging/src/messaging.dart: 83:31) E/颤振(10115): #3主要(包:okepos/主要。dart:130:21) E/扑动(10115):

下面是我的代码:
Future<dynamic> myBackgroundMessageHandler(RemoteMessage message) {
print('backgroundMessage: message => ${message.toString()}');
}

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage((message) => myBackgroundMessageHandler(message));
}

您可能想尝试以以下方式编写主函数。如果你想在后台运行你的函数,你可能需要初始化Firebase。

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
if (message.notification != null) {
print(message.notification.title);
print(message.notification.body);
}
// Create a notification for this
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// Background Message Handler
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}

如果你想看到我的Firebase Messaging with Flutter的实现,检查我的github repo。

最新更新