BaseMessage.fromMap constructor

BaseMessage.fromMap(
  1. dynamic map
)

Implementation

factory BaseMessage.fromMap(dynamic map) {
  if (map == null) throw ArgumentError('The type of basemessage map is null');

  final String category = map['category'] ?? '';

  if (category.isEmpty) {
    throw Exception('Category is missing in JSON');
  }
  if (category == '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 {
      // Custom message
      throw UnimplementedError();
    }
  } else if (category == 'action') {
    return Action.fromMap(map);
  } else if (category == 'call') {
    return Call.fromMap(map);
  } else if (category == 'custom') {
    return CustomMessage.fromMap(map);
  } else {
    throw ArgumentError();
  }
}