ResponseResult.fromJson constructor

ResponseResult.fromJson(
  1. JSONMap json
)

Implementation

factory ResponseResult.fromJson(JSONMap json) {
  final id = json['id'] ?? -1;
  final cmd = json['cmd'] ?? json['error'];
  //TODO! Add handlers for injectScript & redirect
  switch (cmd) {
    case 'response':
      final msg = Message.fromJson(json);
      if (msg.result is Map) {
        if (msg.result['error'] != null) {
          return ResponseResult(
            id: id,
            type: ResponseType.error,
            error: Error.fromJson(msg.result),
          );
        }
      }
      return ResponseResult(
        id: id,
        type: ResponseType.message,
        message: msg,
      );
    case 'error':
      return ResponseResult(
        id: id,
        type: ResponseType.error,
        error: Error.fromJson(json),
      );
    case 'confirm':
    case 'notification':
    case 'progress':
    case 'prompt':
      return ResponseResult(
        id: id,
        type: ResponseType.prompt,
        prompt: PromptResult.fromJson(json),
      );
    default:
      if (cmd is String && cmd.startsWith('set')) {
        return ResponseResult(
          id: id,
          type: ResponseType.event,
          event: EventResult.fromJson(json),
        );
      }
      return ResponseResult(
        id: id,
        type: ResponseType.unknown,
        json: json,
      );
  }
}