ApiResult<T> class

A generic wrapper class used to standardize and simplify how API responses are represented across the app.

This class is useful when you want to unify success, and failure results under a single type. It’s especially handy for working with Dio or repository layers where network errors must be caught gracefully.

Example:

final result = await apiService.getUser();

if (result.isSuccess) {
  print('User data: ${result.data}');
} else {
  print('Error: ${result.message}');
}

Constructors

ApiResult.failure(String error, {int? statusCode})
Creates a failed API result with an error message.
factory
ApiResult.idle()
Creates an idle state result.
factory
ApiResult.success(T? data, {int? statusCode})
Creates a successful API result containing data.
factory

Properties

data → T?
The parsed or decoded data returned from the API.
final
hashCode → int
The hash code for this object.
no setterinherited
isFailure → bool
Returns true if the request failed.
no setter
isIdle → bool
Returns true if this result is in its initial idle state (neither a success nor a failure).
no setter
isSuccess → bool
Returns true if the request completed successfully.
no setter
message → String?
The error message (if any) returned by the API or generated locally.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
statusCode → int?
The HTTP status code of the API response (if available).
final

Methods

map<R>(R transform(T data)) → ApiResult<R>
Convenience method to transform the data field (for example, mapping DTOs to domain models).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
Converts this result into a human-readable string for logging/debugging.
override

Operators

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