0

#I am trying to save the notification title and body locally and fetch the title and body inside my app to show all the sent notification in a listview but i am unable to

#this is the code i am running this code in my main.dart inside initstate and that detail.add is my list which i created manually but i am getting error saying 102:45: Error: This expression has type 'void' and can't be used.prefs.setString('notificationData', setData); when i try to setString this is the error i get

List<String?> detail = [];
FirebaseMessaging.onMessage.listen((message) async{
      if(message.notification!=null){
        // print(message.notification!.body);
        // print(message.notification!.title);
        final title = message.notification?.title;
        final body = message.notification?.body;
         SharedPreferences prefs = await SharedPreferences.getInstance();
         final String notificationData = json.encode({"title":title,"body":body});
         final setData =  detail.add(notificationData);
        prefs.setString('notificationData', setData);
        print(notificationData);
        print(setData);
      }
Saurav
  • 132
  • 1
  • 12

1 Answers1

0

The detail.add(notificationData) function returns nothing (void) and only adds to the existing array, so your setData variable is empty but you can use the original updated detail array like so:

FirebaseMessaging.onMessage.listen((message) async{
  if(message.notification!=null){
    // print(message.notification!.body);
    // print(message.notification!.title);
    final title = message.notification?.title;
    final body = message.notification?.body;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final String notificationData = json.encode({"title":title,"body":body});
    detail.add(notificationData);
    prefs.setString('notificationData', detail.toString());
    print(notificationData);
    print(detail);
  }
//...
});
Roaa
  • 1,212
  • 7
  • 11
  • Thnaks it worked but now i am trying to getString and show it in listTile or listview like this Future getNotification() async { SharedPreferences prefs = await SharedPreferences.getInstance(); if(prefs.containsKey("notificationData")){ prefs.getString('notificationData'); }else{ print("No Notification to show"); } – Saurav Jan 04 '22 at 08:48
  • but i am not getting the data and sorry for trouble – Saurav Jan 04 '22 at 08:53
  • but when i save my notification in details then the new notification replaces the old notification – Saurav Jan 04 '22 at 09:00
  • "but when i save my notification in details then the new notification replaces the old notification" => you need to first assign your `detail` array's value to the notifications already stored in your storage then add to it. It's being replaced because you're initializing it to an empty array at first – Roaa Jan 04 '22 at 09:02
  • And no trouble at all :) . If you want add your new issues to the question with proper code formatting so I can add to my answer. – Roaa Jan 04 '22 at 09:03
  • i was unable to ask a question the problem occured is that the notification which is saved using setString then new notification is replacing the old once and i also wanna get that list of notification inside my app in a card view or list view – Saurav Jan 04 '22 at 09:15
  • Please check out my previous comment – Roaa Jan 04 '22 at 10:45
  • https://stackoverflow.com/questions/70577824/trying-to-save-push-notification-using-shared-preference-and-get-that-title-and – Saurav Jan 04 '22 at 11:08
  • i asked the question in proper format can u please check it? thanks – Saurav Jan 04 '22 at 11:08