ApiResponse<T> class
ApiResponse 实现示例 这是一个可选的实现示例,展示如何继承 Response 抽象类 用户可以根据自己的需求创建自己的响应类
此实现假设响应结构为:{code: int, message: String, data: dynamic}
Constructors
- ApiResponse({required int code, required String message, dynamic data, bool? isSuccess, int? httpStatusCode})
- 构造函数
Properties
- code → int
-
final
- data → T?
-
数据(如果成功)
no setteroverride
- errorCode → int?
-
错误码(如果失败,业务错误码)
默认返回 null,用户可以在自己的响应类中重写此方法返回具体的错误码
no setteroverride
- errorHandled → bool
-
错误是否已被处理(通过链式调用的 onFailure 回调或 send 的 onFailure 回调)
如果为 true,表示错误已经通过错误处理回调处理,不应该再调用全局的 onFailure
no setterinherited
- errorMessage → String?
-
错误消息(如果失败)
no setteroverride
- hashCode → int
-
The hash code for this object.
no setterinherited
- httpStatusCode → int?
-
HTTP 状态码(如 200, 404, 500 等)
默认返回 null,用户可以在自己的响应类中重写此方法返回 HTTP 状态码
no setteroverride
- isSuccess → bool
-
是否成功
final
- message → String
-
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
extract<
R> (R? extractor(T? data)) → R? -
提取数据(自动处理类型转换)
仅在成功时执行提取器
内部已处理异常,用户不需要 try-catch
inherited
-
extractField<
R> (String key) → R? -
从 Map 中提取字段(最简单的方式)
适用于从 Map<String, dynamic> 中直接获取字段值
inherited
-
extractList<
R> (String key, R fromJson(Map< String, dynamic> json)) → List<R> -
从 Map 中提取列表字段并转换为模型列表
适用于从 Map<String, dynamic> 中提取 List 字段并转换为模型列表
inherited
-
extractModel<
R> (R? fromJson(Map< String, dynamic> json)) → R? -
从 Map 提取模型(类型安全,自动处理类型检查和异常)
适用于从 Map<String, dynamic> 转换为模型类
inherited
-
extractPath<
R> (String path) → R? -
从 Map 中提取嵌套字段(支持路径,如 'user.name')
适用于从 Map<String, dynamic> 中提取嵌套字段值
inherited
-
getData(
) → T? -
获取数据(类型安全)
inherited
-
handleError(
) → void -
自动处理错误(失败时自动显示错误消息)
注意:错误处理现在统一由 HttpConfig.onFailure 处理
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onFailure(
void callback(int? httpStatusCode, int? errorCode, String message)) → Response< T> -
失败时执行回调
返回自身,支持链式调用
注意:如果调用了此方法,错误将被标记为已处理,全局的 onFailure 不会再被调用
inherited
-
onSuccess(
VoidCallback callback) → Response< T> -
成功时执行回调
返回自身,支持链式调用
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited