call<R> method

Future<R?> call<R>(
  1. String method, [
  2. dynamic params
])

Calls or executes a method by its name.

If method is not registered, it will throw RpcError of code RpcError.kMethodNotFound

If method is registered, it will call the callback and return its result of type R as a Future.

If an error is thrown by the callback, it will be wrapped in RpcError of code RpcError.kInternalError and thrown.

Implementation

Future<R?> call<R>(String method, [dynamic params]) async {
  final callback = _methods[prefix + method];
  if (callback == null) {
    throw RpcError(
      code: RpcError.kMethodNotFound,
      message: 'Method not found',
      data: {
        'method': prefix + method,
        'registered_methods': _methods.keys.toList(),
      },
    );
  }

  try {
    final R? result = await callback(params);
    return result;
  } catch (err, stk) {
    throw RpcError(
      code: RpcError.kInternalError,
      message: 'Internal Error',
      data: {
        'method': prefix + method,
        'params': params,
        'error': err.toString(),
        'stacktrace': stk.toString(),
      },
    );
  }
}