I'm trying to add the data from a Firebase Cloud Messaging notification to a list.

final List<FCMContent> fcmcrafts = [];

The list is created from JSON data that has been downloaded from an external PHP page.

if (response.body.contains('audioid')) {
    List fcmcrafts = json.decode(response.body);
    return fcmcrafts.map((fcmcraft) => FCMContent.fromJson(fcmcraft)).toList();
}

Here's the class I've got for FCMContent;

class FCMContent {

  final String audioid, name, title;

  FCMContent({
    this.audioid,
    this.name,
    this.title,
  });

  factory FCMContent.fromJson(Map<String, dynamic> jsonData) =>FCMContent(
      audioid: jsonData['audioid'],
      name: jsonData['name'],
      title: jsonData['title']
  );

  Map<String, dynamic> toJson() => {
    "audioid":audioid,
    "name":name,
    "title":title
  };
}

How do I add the FCM notification to the list? Here's how I would add it if it were a string.

 @override
 void onNotify(RemoteMessage notification) {
  _firebaseMessagingBackgroundHandler(notification);
 setState(() {
  _notification = notification;
  fcmcrafts.insert(0, _notification?.notification?.title ?? "");
  });
}

But how do I add the FCM notification which arrives as JSON code?


Solution 1: ahmetakil

Can't you do something like this

fcmcrafts.insert(0, FCMContent.fromJson(_notification),);

you simply need to convert RemoteMessage to FCMContent maybe you can create a custom constructor FCMContent.fromRemoteMessage() and map the fields.