---
title: Resource API docs
description: All the API docs of Resource
---

# Resource API docs

`Resource`s are special `Signal`s designed specifically to handle __Async__
loading. Their purpose is wrap async values in a way that makes them easy
to interact with handling the common states of a future or stream __data__, __error__ and __loading__.

A __Resource__ extends a [Signal](/api-docs/signal), so all the API of __Signal__ are available.

## Constructors

Future-based constructor:

```dart
Resource(
  Future<T> Function() fetcher, {
  SignalBase<dynamic>? source,
  String? name,
  bool? equals,
  bool? autoDispose,
  bool? trackInDevTools,
  bool lazy = true,
});
```

Stream-based constructor:

```dart
Resource.stream(
  Stream<T> Function() stream, {  
  SignalBase<dynamic>? source,
  String? name,
  bool? equals,
  bool? autoDispose,
  bool? trackInDevTools,
  bool lazy = true,
});
```

`fetcher` is the function that returns a `Future`.
`stream` is the function that returns a `Stream`.
`source` is the query to the async data `fetcher` or `stream` function that triggers the resource everytime it changes.
the other parameters are the options of the resource.

```dart
// Using http as a client, you can use any other client
import 'package:http/http.dart' as http;

// The source
final userId = Signal(1);

// The fetcher
Future<String> fetchUser() async {
  final response = await http.get(
    Uri.parse('https://jsonplaceholder.typicode.com/users/${userId.value}/'),
    headers: {'Accept': 'application/json'},
  );
  return response.body;
}

// The resource (source is optional)
final user = Resource(fetchUser, source: userId);
```

In the example above, just changing the `userId` value will trigger the `Resource` again.

## `ResourceState<T> get state`

Returns the current state of the resource.
See [ResourceState](/api-docs/resource-state) for more details.

## `ResourceState<T> get previousState`

Returns the previous state of the resource.
See [ResourceState](/api-docs/resource-state) for more details.

## `Future<void> refresh()`

Forces a refresh of the resource.

## `Resource<Selected> select<Selected>(Selected Function(T data) selector, { String? name })`

Filters the `Resource`'s data by reading only the properties that you care about.

The advantage is that you keep handling the loading and error states.

## `FutureOr<T> untilReady()`

Returns a future that completes with the value when the Resource is ready
If the resource is already ready, it completes immediately.

## `ResourceState<T> update(ResourceState<T> Function(ResourceState<T> state) callback)`

Calls a function with the current [state] and assigns the result as the new state.
