useRequest<T> function

RequestController<T> useRequest<T>(
  1. Future<T> service(), {
  2. RequestOptions<T> options = const RequestOptions(),
})

A composable for managing asynchronous data fetching.

It simplifies the process of handling loading states, errors, and data fetching logic. It is inspired by VueUse's useRequest.

Example

// In your widget
final controller = useRequest(() => fetchUserData());

// In your build method
if (controller.loading.value) {
  return CircularProgressIndicator();
}

if (controller.error.value != null) {
  return Text('Error: ${controller.error.value}');
}

return Text('User: ${controller.data.value}');

service is the asynchronous function that fetches the data. options are the options for the request, such as manual triggering.

Implementation

RequestController<T> useRequest<T>(
  Future<T> Function() service, {
  RequestOptions<T> options = const RequestOptions(),
}) {
  return RequestController(service, options);
}