HttpResultHandler class

A thin, opt-in adapter that wraps package:http calls in Result values.

Built for quick prototypes and small apps: zero ceremony, no interceptors, no transformers. For production HTTP clients, prefer the Dio integration (DioResultHandler) which exposes interceptors, file transfers, and richer error mapping.

final api = HttpResultHandler(
  baseUrl: 'https://api.example.com',
  defaultHeaders: const {'Accept': 'application/json'},
);

final user = await api.get<User>(
  '/users/me',
  parser: (json) => User.fromJson(json! as Map<String, dynamic>),
);
user.when(
  success: print,
  error: (f) => print('failed: ${f.message}'),
);

Constructors

HttpResultHandler({String baseUrl = '', Map<String, String> defaultHeaders = const <String, String>{}, Duration timeout = const Duration(seconds: 30), Client? client})
Creates a new handler.

Properties

baseUrl String
Prefix prepended to every relative path passed to the verb methods.
final
defaultHeaders Map<String, String>
Headers applied to every request, merged with per-call headers.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
timeout Duration
Per-request timeout. A timeout produces Failure.timeout.
final

Methods

close() → void
Closes the underlying HTTP client if this handler created it.
delete<T>(String path, {Object? body, Map<String, String>? headers, Map<String, dynamic>? queryParameters, required JsonParser<T> parser}) Future<Result<T>>
Performs a DELETE.
get<T>(String path, {Map<String, String>? headers, Map<String, dynamic>? queryParameters, required JsonParser<T> parser}) Future<Result<T>>
Performs a GET and decodes the response body via parser.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch<T>(String path, {Object? body, Map<String, String>? headers, Map<String, dynamic>? queryParameters, required JsonParser<T> parser}) Future<Result<T>>
Performs a PATCH with a JSON-encoded body.
post<T>(String path, {Object? body, Map<String, String>? headers, Map<String, dynamic>? queryParameters, required JsonParser<T> parser}) Future<Result<T>>
Performs a POST with a JSON-encoded body.
put<T>(String path, {Object? body, Map<String, String>? headers, Map<String, dynamic>? queryParameters, required JsonParser<T> parser}) Future<Result<T>>
Performs a PUT with a JSON-encoded body.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited