getAs<T> method

  1. @override
Future<Either<Failure, T>> getAs<T>({
  1. required String endpoint,
  2. required T parser(
    1. dynamic data
    ),
  3. Object? data,
  4. Map<String, dynamic>? params,
  5. Duration? receiveTimeout,
  6. Duration? sendTimeout,
  7. Map<String, String>? headers,
  8. ProgressCallback? onSendProgress,
  9. ProgressCallback? onReceiveProgress,
  10. CancelToken? cancelToken,
  11. bool showLoader = true,
  12. CachePolicy? cachePolicy,
  13. Duration? cacheMaxAge,
  14. bool dedupe = true,
})
override

get, with the response body deserialized into T.

parser receives response.data — already-decoded JSON, so usually a Map<String, dynamic> or a List. If it throws, the result is a Failure with ErrorSource.parseError carrying the raw body in failure.data; the exception never reaches the caller.

final user = await _api.getAs<User>(
  endpoint: '/users/1',
  parser: User.fromJson,
);

final users = await _api.getAs<List<User>>(
  endpoint: '/users',
  parser: listParser(User.fromJson),
);

Implementation

@override
Future<Either<Failure, T>> getAs<T>({
  required String endpoint,
  required T Function(dynamic data) parser,
  Object? data,
  Map<String, dynamic>? params,
  Duration? receiveTimeout,
  Duration? sendTimeout,
  Map<String, String>? headers,
  ProgressCallback? onSendProgress,
  ProgressCallback? onReceiveProgress,
  CancelToken? cancelToken,
  bool showLoader = true,
  CachePolicy? cachePolicy,
  Duration? cacheMaxAge,
  bool dedupe = true,
}) {
  return _respondAs(HttpMethod.get, endpoint, parser,
      data: data, params: params, headers: headers);
}