Response<T> class
abstract
响应接口 用户必须实现此接口来定义自己的响应结构
工具类通过此接口提供统一的便利方法(如 onSuccess, onFailure, handleError) 但响应类的具体结构完全由用户定义
示例:
class MyResponse<T> implements Response<T> {
final bool success;
final String? error;
final T? payload;
MyResponse({required this.success, this.error, this.payload});
@override
bool get isSuccess => success;
@override
String? get errorMessage => error;
@override
T? get data => payload;
}
- Implementers
Constructors
- Response()
Properties
- data → T?
-
数据(如果成功)
no setter
- errorCode → int?
-
错误码(如果失败,业务错误码)
默认返回 null,用户可以在自己的响应类中重写此方法返回具体的错误码
no setter
- errorHandled → bool
-
错误是否已被处理(通过链式调用的 onFailure 回调或 send 的 onFailure 回调)
如果为 true,表示错误已经通过错误处理回调处理,不应该再调用全局的 onFailure
no setter
- errorMessage → String?
-
错误消息(如果失败)
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- httpStatusCode → int?
-
HTTP 状态码(如 200, 404, 500 等)
默认返回 null,用户可以在自己的响应类中重写此方法返回 HTTP 状态码
no setter
- isSuccess → bool
-
是否成功
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
extract<
R> (R? extractor(T? data)) → R? - 提取数据(自动处理类型转换) 仅在成功时执行提取器 内部已处理异常,用户不需要 try-catch
-
extractField<
R> (String key) → R? - 从 Map 中提取字段(最简单的方式) 适用于从 Map<String, dynamic> 中直接获取字段值
-
extractList<
R> (String key, R fromJson(Map< String, dynamic> json)) → List<R> - 从 Map 中提取列表字段并转换为模型列表 适用于从 Map<String, dynamic> 中提取 List 字段并转换为模型列表
-
extractModel<
R> (R? fromJson(Map< String, dynamic> json)) → R? - 从 Map 提取模型(类型安全,自动处理类型检查和异常) 适用于从 Map<String, dynamic> 转换为模型类
-
extractPath<
R> (String path) → R? - 从 Map 中提取嵌套字段(支持路径,如 'user.name') 适用于从 Map<String, dynamic> 中提取嵌套字段值
-
getData(
) → T? - 获取数据(类型安全)
-
handleError(
) → void - 处理错误(可选实现,工具类会调用此方法) 默认实现为空,用户可以在自己的响应类中重写
-
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 不会再被调用
-
onSuccess(
VoidCallback callback) → Response< T> - 成功时执行回调 返回自身,支持链式调用
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited