MBMessage.fromDictionary constructor

MBMessage.fromDictionary(
  1. Map<String, dynamic> dictionary
)

Initializes a message with the dictionary returned by the APIs.

Implementation

factory MBMessage.fromDictionary(Map<String, dynamic> dictionary) {
  int id = dictionary['id'] is int ? dictionary['id'] : 0;
  String title = dictionary['title'] is String ? dictionary['title'] : '';
  String messageDescription =
      dictionary['description'] is String ? dictionary['description'] : '';

  String? typeString = dictionary['type'];
  MBMessageType messageType = _messageTypeFromString(typeString);

  MBInAppMessage? inAppMessage;
  MBPushMessage? pushMessage;
  Map<String, dynamic>? content = dictionary['content'];
  if (content != null) {
    if (messageType == MBMessageType.inAppMessage) {
      inAppMessage = MBInAppMessage.fromDictionary(content);
    } else if (messageType == MBMessageType.push) {
      pushMessage = MBPushMessage.fromDictionary(content);
    }
  }

  int creationDateInt = dictionary['created_at'] ?? 0;
  DateTime creationDate =
      DateTime.fromMillisecondsSinceEpoch(creationDateInt * 1000);

  int? startDateInt = dictionary['starts_at'];
  DateTime? startDate;
  if (startDateInt != null) {
    startDate = DateTime.fromMillisecondsSinceEpoch(startDateInt * 1000);
  }

  int? endDateInt = dictionary['ends_at'];
  DateTime? endDate;
  if (endDateInt != null) {
    endDate = DateTime.fromMillisecondsSinceEpoch(endDateInt * 1000);
  }

  bool automationIsOn = false;
  if (dictionary['automation'] is int) {
    automationIsOn = dictionary['automation'] == 1;
  } else if (dictionary['automation'] is bool) {
    automationIsOn = dictionary['automation'] ?? false;
  }

  int sendAfterDays = dictionary['send_after_days'] is int
      ? dictionary['send_after_days']
      : 0;
  int repeatTimes = dictionary['repeat'] is int ? dictionary['repeat'] : 1;

  dynamic triggers = dictionary['triggers'];

  return MBMessage(
    id: id,
    title: title,
    messageDescription: messageDescription,
    messageType: messageType,
    inAppMessage: inAppMessage,
    pushMessage: pushMessage,
    createdAt: creationDate,
    startDate: startDate,
    endDate: endDate,
    automationIsOn: automationIsOn,
    sendAfterDays: sendAfterDays,
    repeatTimes: repeatTimes,
    triggers: triggers,
  );
}