#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);
}
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);
}
//...
});