BaseMessage.fromJson constructor Null safety

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

Map json object

Implementation

factory BaseMessage.fromJson(Map<String, dynamic> json) {
  final cmd = json['type'] as String;
  BaseMessage msg;
  //basemessage backward compatibility -
  if (json['custom'] != null) json['data'] = json['custom'];
  if (json['ts'] != null) json['created_at'] = json['ts'];
  if (json['msg_id'] != null) json['message_id'] = json['msg_id'];

  if (CommandType.isUserMessage(cmd)) {
    msg = UserMessage.fromJson(json);
  } else if (CommandType.isFileMessage(cmd)) {
    msg = FileMessage.fromJson(json);
  } else if (CommandType.isAdminMessage(cmd)) {
    msg = AdminMessage.fromJson(json);
  } else {
    throw UnrecognizedMessageTypeError();
  }

  final metaArray = json['metaarray'];
  final metaArrayKeys = List<String>.from(json['metaarray_key_order'] ?? []);
  //NOTE: sorted_metaarray is from API, metaarray list is local,
  //metaarray map is from Web, so had to handle separately

  //local cmd case
  if (metaArray is List) {
    msg.metaArrays = metaArray
        .map((e) => MessageMetaArray.fromJson(e as Map<String, dynamic>))
        .toList();
  }
  //websocket cmd result case
  else if (metaArray is Map && metaArrayKeys.isNotEmpty) {
    msg.metaArrays = metaArrayKeys.map((e) {
      final value = List<String>.from(metaArray[e]);
      return MessageMetaArray(key: e, value: value);
    }).toList();
  }

  return msg;
}