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
errormessage.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
trueif there is an error message.no setter - isSuccess → bool
-
Returns
trueif the result contains data.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
datafield (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