In this current snippet I have declared notification to be nullable but get an null safe error for the if loop's notification.body and notification.title.
Here's my code:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
debugPrint('A new onMessageOpenedApp event was published!');
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text(notification.title),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification.body)],
),
),
);
});
}
});
If I make 'notification' to be non nullable, I get the same null safe error.
Code:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
debugPrint('A new onMessageOpenedApp event was published!');
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text(notification.title),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification.body)],
),
),
);
});
}
});
How to get this to work?
Any help appreciated!
Edit: Added the method name to the coding part.