Home > Enterprise >  Keep getting RemoteNotification A value of type 'RemoteNotification?' can't be assign
Keep getting RemoteNotification A value of type 'RemoteNotification?' can't be assign

Time:02-04

I am inserting Firebase Cloud Messaging for push notification on my Flutter app but I am having trouble with the RemoteNotification that says "A value of type 'RemoteNotification?' can't be assigned to a variable of type 'RemoteNotification'."

I tried adding "?" on the RemoteNotofication but I am having errors on my AlertDialog title and body as I put notification.title inside it.

here is my code

void initState() {
    super.initState();
    FirebaseMessaging.onMessage.listen((RemoteMessage message){
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;
      if(notification != null && android != null){
        flutterLocalNotificationsPlugin.show(notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channel.description,
                color: Colors.blue,
                playSound: true,
                icon: 'assets/bluelogo.png',
              ),
            )
        );
      }
    });
    FirebaseMessaging.onMessage.listen((RemoteMessage message){
     print('A new onMessageOpenedApp event was puclished!');
     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)
               ],
             ),
           ),
         );
       });
     }
    });
  }

CodePudding user response:

After adding '?' in RemoteNotification add '??' in text widget and default text as below code

RemoteNotification? notification = message.notification;

return AlertDialog(
       title: Text(notification?.title ?? 'Default title'),//<-here
       content: SingleChildScrollView(
         child: Column(
           crossAxisAlignment: CrossAxisAlignment.start,
           children: [
             Text(notification?.body ?? 'Default body')//<-here
           ],
         ),
       ),

CodePudding user response:

I've noticed you have assigned 2 listeners to the same event... By your print statement I think you meant to write a listener for onMessageOpenedApp? Do check that out.

Coming to your error, yes You have to keep RemoteNotification nullable since it can be null.

Now the showDialog() should be called only if not null . The title and body can be null too so make them null aware.

An example :

  FirebaseMessaging.onMessageOpenedApp.listen(
(RemoteMessage message){
     print('A new onMessageOpenedApp event was puclished!');
     RemoteNotification? notification = message.notification; //make null aware
     AndroidNotification? android = message.notification?.android;//make null aware
     if (notification != null && android != null) {
       showDialog(context: context, builder: (_) {
         return AlertDialog(
            title: Text(notification.title??'No Title'),
              content: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(notification.body??'Body')
                  ],
                ),
              ),
         );
       });
     }
    }
);
  • Related