Message.fromJson constructor

Message.fromJson(
  1. Map<String, dynamic> json
)

Creates a particular message from a map (decoded JSON). Type is determined by the type field.

Implementation

factory Message.fromJson(Map<String, dynamic> json) {
  final type = MessageType.values.firstWhere(
    (e) => e.name == json['type'],
    orElse: () => MessageType.unsupported,
  );

  switch (type) {
    case MessageType.audio:
      return AudioMessage.fromJson(json);
    case MessageType.custom:
      return CustomMessage.fromJson(json);
    case MessageType.file:
      return FileMessage.fromJson(json);
    case MessageType.image:
      return ImageMessage.fromJson(json);
    case MessageType.system:
      return SystemMessage.fromJson(json);
    case MessageType.text:
      return TextMessage.fromJson(json);
    case MessageType.unsupported:
      return UnsupportedMessage.fromJson(json);
    case MessageType.video:
      return VideoMessage.fromJson(json);
  }
}