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.
A couple things going on here. First, the reason you are getting the null error:
When
notificationis a nullable value, you will get the warning when you attempt to use it without first checking if it is null. Changing it to be non-nullable can resolve the problem, but potentially in a way you don't actually want.Now when you changed it to non-nullable, you didn't change the line after it:
Now
notificationis no longer nullable, but you still use a null-coalescing operator when creatingandroid. That means that, even thoughnotificationis non-nullable,androidstill is nullable. From this, you will get two warnings - one for trying to coalesce a non-nullable value, and one for usingandroidwithout checking for null.The easiest solution would be to make it so
androidis non-nullable as well:But this might not be possible depending on whether
message.notificationitself is nullable.Now onto the second thing. Assuming we want
notificationandandroidto be nullable, your first null-check isn't enough to eliminate the warning within the callback toshowDialog. This is because, even though it is known that whenshowDialogis called,notificationis not null, the analyzer can't know when the callback itself will be called, and by that point,notificationmight have become null. For that reason, the analyzer has no choice but to display the warning regardless of your current null check.There are four ways to resolve this. First, you could check for null again:
Or force non-null with the
!operator:Or provide default values in the case that the objects are null:
Or make non-null versions of the objects that the callback can closure around: