I am trying to implement notifications for my app, but when initializing notifications FirebaseMessaging.onBackgroundMessage((message) => myBackgroundMessageHandler(message))gives the following error. I have looked through this issue and this issue and have taken the function myBackgroundMessageHandler that I'm using as an argument outside the class but the error still persists.
Below is the error:
E/flutter (10115): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value E/flutter (10115): #0
MethodChannelFirebaseMessaging.registerBackgroundMessageHandler (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:179:53) E/flutter (10115): #1
FirebaseMessagingPlatform.onBackgroundMessage= (package:firebase_messaging_platform_interface/src/platform_interface/platform_interface_messaging.dart:101:16) E/flutter (10115): #2 FirebaseMessaging.onBackgroundMessage (package:firebase_messaging/src/messaging.dart:83:31) E/flutter (10115): #3 main (package:okepos/main.dart:130:21) E/flutter (10115):
Below is my code:
Future<dynamic> myBackgroundMessageHandler(RemoteMessage message) {
print('backgroundMessage: message => ${message.toString()}');
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage((message) => myBackgroundMessageHandler(message));
}