UseRequestResult<TData, TParams> class
useRequest 的返回结果对象
包含请求状态和操作方法,提供完整的请求控制能力。
状态属性
- loading - 是否正在加载
- data - 请求返回的数据
- error - 错误信息
- params - 当前请求的参数
- loadingMore - 是否正在加载更多
- hasMore - 是否还有更多数据
操作方法
- run/runAsync - 执行请求
- refresh/refreshAsync - 刷新(使用上次参数)
- loadMore/loadMoreAsync - 加载更多
- mutate - 直接修改数据
- cancel - 取消请求
示例
final result = useRequest<User, int>(fetchUser);
// 根据状态渲染 UI
if (result.loading) {
return CircularProgressIndicator();
}
if (result.error != null) {
return Text('Error: ${result.error}');
}
return Text('User: ${result.data?.name}');
Constructors
-
UseRequestResult({required bool loading, required TData? data, required dynamic error, required TParams? params, bool loadingMore = false, bool? hasMore, bool isPolling = false, required Future<
TData> runAsync(TParams params), required void run(TParams params), required Future<TData> refreshAsync(), required void refresh(), Future<TData> loadMoreAsync()?, void loadMore()?, required void mutate(TData? mutator(TData? oldData)?), required void cancel(), void pausePolling()?, void resumePolling()?}) -
const
Properties
- cancel → void Function()
-
取消当前进行中的请求
final
- data → TData?
-
返回数据
final
- error → dynamic
-
错误信息
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasMore → bool?
-
是否还有更多数据
final
- isPolling → bool
-
是否正在轮询
final
- loading → bool
-
是否处于加载中
final
- loadingMore → bool
-
是否处于加载更多中
final
- loadMore → VoidCallback?
-
加载更多(不等待返回)
final
-
loadMoreAsync
→ Future<
TData> Function()? -
加载更多(异步)
final
- mutate → void Function(TData? mutator(TData? oldData)?)
-
直接修改数据(不触发请求)
final
- params → TParams?
-
当前请求的参数
final
- pausePolling → void Function()
-
暂停轮询
final
- refresh → void Function()
-
使用上一次参数进行刷新(不等待返回)
final
-
refreshAsync
→ Future<
TData> Function() -
使用上一次参数进行刷新(异步)
final
- resumePolling → void Function()
-
恢复轮询
final
- run → void Function(TParams params)
-
触发请求(不等待返回)
final
-
runAsync
→ Future<
TData> Function(TParams params) -
异步执行请求
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited