fromMap method

  1. @override
NotificationModel? fromMap(
  1. Map<String, dynamic> mapData
)

Imports data from a serializable object

Implementation

@override
NotificationModel? fromMap(Map<String, dynamic> mapData) {
  try {
    assert(mapData.containsKey(NOTIFICATION_CONTENT) &&
        mapData[NOTIFICATION_CONTENT] is Map);

    Map<String, dynamic> contentData =
        Map<String, dynamic>.from(mapData[NOTIFICATION_CONTENT]);

    _content =
        NotificationContent(id: 0, channelKey: '').fromMap(contentData);
    if (_content == null) return null;

    _content!.validate();

    if (mapData.containsKey(NOTIFICATION_SCHEDULE)) {
      Map<String, dynamic> scheduleData =
          Map<String, dynamic>.from(mapData[NOTIFICATION_SCHEDULE]);

      if (scheduleData.containsKey(NOTIFICATION_SCHEDULE_INTERVAL)) {
        _schedule = NotificationInterval(interval: 0).fromMap(scheduleData);
      } else if (scheduleData.containsKey(NOTIFICATION_CRONTAB_EXPRESSION)) {
        _schedule = NotificationAndroidCrontab().fromMap(scheduleData);
      } else {
        _schedule = NotificationCalendar().fromMap(scheduleData);
      }
      _schedule?.validate();
    }

    if (mapData.containsKey(NOTIFICATION_BUTTONS) &&
        mapData[NOTIFICATION_BUTTONS] != null) {
      _actionButtons = [];
      List<dynamic> actionButtonsData =
          List<dynamic>.from(mapData[NOTIFICATION_BUTTONS]);

      for (dynamic buttonData in actionButtonsData) {
        Map<String, dynamic> actionButtonData =
            Map<String, dynamic>.from(buttonData);

        NotificationActionButton button =
            NotificationActionButton(label: '', key: '')
                .fromMap(actionButtonData) as NotificationActionButton;
        button.validate();

        _actionButtons!.add(button);
      }
      assert(_actionButtons!.isNotEmpty);
    }
  } catch (e) {
    return null;
  }

  return this;
}