MBInAppMessage.fromDictionary constructor

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

Initializes a message with the dictionary returned by the APIs.

Implementation

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

  String? styleString = dictionary['type'];
  MBInAppMessageStyle style = _styleFromString(styleString);

  double duration = -1;
  if (dictionary['duration'] != null) {
    if (dictionary['duration'] is double) {
      duration = dictionary['duration'];
    } else if (dictionary['duration'] is int) {
      int intDuration = dictionary['duration'];
      duration = intDuration.toDouble();
    }
  }

  String? title = dictionary['title'] is String ? dictionary['title'] : null;
  Color? titleColor = _colorFromField(
    dictionary,
    'title_color',
  );

  String? body =
      dictionary['content'] is String ? dictionary['content'] : null;
  Color? bodyColor = _colorFromField(
    dictionary,
    'content_color',
  );

  Color? backgroundColor = _colorFromField(
    dictionary,
    'background_color',
  );

  String? image = dictionary['image'] is String ? dictionary['image'] : null;

  List<MBInAppMessageButton> buttons = [];
  String? button1Title =
      dictionary['cta_text'] is String ? dictionary['cta_text'] : null;
  String? button1TitleColor = dictionary['cta_text_color'] is String
      ? dictionary['cta_text_color']
      : null;
  String? button1BackgroundColor =
      dictionary['cta_background_color'] is String
          ? dictionary['cta_background_color']
          : null;
  String? button1Link =
      dictionary['cta_action'] is String ? dictionary['cta_action'] : null;
  String? button1LinkType = dictionary['cta_action_type'] is String
      ? dictionary['cta_action_type']
      : null;
  if (button1Title != null && button1LinkType != null) {
    int? sectionId;
    int? blockId;
    if (button1LinkType == 'section') {
      Map<String, dynamic>? actionMap =
          _extractSectionAndBlockId(button1Link);
      sectionId = actionMap?['sectionId'];
      blockId = actionMap?['blockId'];
    }
    buttons.add(
      MBInAppMessageButton(
        title: button1Title,
        titleColor: _colorFromHexString(button1TitleColor),
        backgroundColor: _colorFromHexString(button1BackgroundColor),
        link: button1Link,
        sectionId: sectionId,
        blockId: blockId,
        linkTypeString: button1LinkType,
      ),
    );
  }
  String? button2Title =
      dictionary['cta2_text'] is String ? dictionary['cta2_text'] : null;
  String? button2TitleColor = dictionary['cta2_text_color'] is String
      ? dictionary['cta2_text_color']
      : null;
  String? button2BackgroundColor =
      dictionary['cta2_background_color'] is String
          ? dictionary['cta2_background_color']
          : null;
  String? button2Link =
      dictionary['cta2_action'] is String ? dictionary['cta2_action'] : null;
  String? button2LinkType = dictionary['cta2_action_type'] is String
      ? dictionary['cta2_action_type']
      : null;
  if (button2Title != null && button2LinkType != null) {
    int? sectionId;
    int? blockId;
    if (button2LinkType == 'section') {
      Map<String, dynamic>? actionMap =
          _extractSectionAndBlockId(button1Link);
      sectionId = actionMap?['sectionId'];
      blockId = actionMap?['blockId'];
    }
    buttons.add(
      MBInAppMessageButton(
        title: button2Title,
        titleColor: _colorFromHexString(button2TitleColor),
        backgroundColor: _colorFromHexString(button2BackgroundColor),
        link: button2Link,
        sectionId: sectionId,
        blockId: blockId,
        linkTypeString: button2LinkType,
      ),
    );
  }

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

  return MBInAppMessage(
    id: id,
    style: style,
    isBlocking: isBlocking,
    duration: duration,
    title: title,
    titleColor: titleColor,
    body: body,
    bodyColor: bodyColor,
    image: image,
    backgroundColor: backgroundColor,
    buttons: buttons,
  );
}