get<T> method

Future<T> get<T>(
  1. Query<T> query, {
  2. Map<String, String> headers = const {},
})

Send a query to the backend and expect a result of the type T.

Headers provided in headers are on top of the headers from the Cqrs constructor, meaning headers override _headers. Content-Type header will be ignored.

A CqrsException will be thrown in case of an error.

Implementation

Future<T> get<T>(
  Query<T> query, {
  Map<String, String> headers = const {},
}) async {
  final response = await _send(query, pathPrefix: 'query', headers: headers);

  if (response.statusCode == 200) {
    try {
      final dynamic json = jsonDecode(response.body);

      return query.resultFactory(json);
    } catch (e) {
      throw CqrsException(
        response,
        'An error occured while decoding response body JSON:\n$e',
      );
    }
  }

  throw CqrsException(
    response,
    'Invalid, non 200 status code returned by ${query.getFullName()} query.',
  );
}