call method

  1. @override
Future<RPCResponse> call(
  1. String function, [
  2. List? params
])
override

Performs an RPC request, asking the server to execute the function with the given name and the associated parameters, which need to be encodable with the json class of dart:convert.

When the request is successful, an RPCResponse with the request id and the data from the server will be returned. If not, an RPCError will be thrown. Other errors might be thrown if an IO-Error occurs.

Implementation

@override
Future<RPCResponse> call(String function, [List<dynamic>? params]) async {
  params ??= [];

  final requestPayload = {
    'jsonrpc': '2.0',
    'method': function,
    'params': params,
    'id': _currentRequestId++,
  };

  final response = await client.post(
    Uri.parse(url),
    headers: {'Content-Type': 'application/json'},
    body: json.encode(requestPayload),
  );

  final data = json.decode(response.body) as Map<String, dynamic>;
  final id = data['id'] as int;

  if (data.containsKey('error')) {
    final error = data['error'];

    final code = error['code'] as int;
    final message = error['message'] as String;
    final errorData = error['data'];

    throw RPCError(code, message, errorData);
  }

  final result = data['result'];
  return RPCResponse(id, result);
}