BaseMessage.fromMap constructor

BaseMessage.fromMap(
  1. dynamic map
)

Creates a new BaseMessage instance from a map.

Implementation

factory BaseMessage.fromMap(dynamic map) {
  if (map == null) {
    throw ArgumentError('The type of base message map is null');
  }
  final String category = map['category'] ?? '';
  if (category.isEmpty) {
    throw Exception('Category is missing in JSON');
  }
  if (category == CometChatMessageCategory.message) {
    if (map['type'] == 'text') {
      return TextMessage.fromMap(map);
    } else if (map['type'] == CometChatMessageType.file ||
        map['type'] == CometChatMessageType.image ||
        map['type'] == CometChatMessageType.video ||
        map['type'] == CometChatMessageType.audio) {
      return MediaMessage.fromMap(map);
    } else {
      throw UnimplementedError();
    }
  } else if (category == CometChatMessageCategory.action) {
    return Action.fromMap(map);
  } else if (category == CometChatMessageCategory.call) {
    return Call.fromMap(map);
  } else if (category == CometChatMessageCategory.custom) {
    return CustomMessage.fromMap(map);
  } else if (category == CometChatMessageCategory.interactive) {
    return InteractiveMessage.fromMap(map);
  } else {
    throw ArgumentError();
  }
}