get<_Model extends RestModel> method

  1. @override
Future<List<_Model>> get<_Model extends RestModel>({
  1. Query? query,
  2. ModelRepository<RestModel>? repository,
})

Query's providerArgs can extend the get functionality:

  • 'headers' (Map<String, String>) set HTTP headers
  • 'topLevelKey' (String) includes the incoming payload beneath a JSON key (For example, {"user": {"id"...}}). It is recommended to use RestSerializable#fromKey instead to simplify queries (however, when defined, topLevelKey is prioritized). Note that when no key is defined, the first value is returned regardless of the first key (in the example, {"id"...}).

Implementation

@override
Future<List<_Model>> get<_Model extends RestModel>({query, repository}) async {
  final url = urlForModel<_Model>(query);
  if (url == null) return <_Model>[];

  logger.fine('GET $url');

  final adapter = modelDictionary.adapterFor[_Model]!;
  final resp = await client.get(Uri.parse(url), headers: headersForQuery(query));

  logger.finest('#get: url=$url statusCode=${resp.statusCode} body=${resp.body}');

  if (statusCodeIsSuccessful(resp.statusCode)) {
    final topLevelKey = (query?.providerArgs ?? {})['topLevelKey'] ?? adapter.fromKey;
    final parsed = convertJsonFromGet(resp.body, topLevelKey);
    final body = parsed is Iterable ? parsed : [parsed];
    final results = body
        .where((msg) => msg != null)
        .map((msg) {
          return adapter.fromRest(msg, provider: this, repository: repository);
        })
        .toList()
        .cast<Future<_Model>>();

    return await Future.wait<_Model>(results);
  } else {
    logger.warning('#get: url=$url statusCode=${resp.statusCode} body=${resp.body}');
    throw RestException(resp);
  }
}