selectAsync<Output> method

AlwaysAliveProviderListenable<Future<Output>> selectAsync<Output>(
  1. Output selector(
    1. T data
    )
)
inherited

A variant of select for asynchronous values

selectAsync is useful for filtering rebuilds of a provider when it depends on asynchronous values, which we want to await.

A common use-case would be to combine selectAsync with FutureProvider to perform an async operation, where that async operation depends on the result of another async operation.

// A provider which asynchronously loads configurations,
// which may change over time.
final configsProvider = StreamProvider<Config>((ref) async {
  // TO-DO fetch the configurations, such as by using Firebase
});

// A provider which fetches a list of products based on the configurations
final productsProvider = FutureProvider<List>((ref) async {
  // We obtain the host from the configs, while ignoring changes to
  // other properties. As such, the productsProvider will rebuild only
  // if the host changes
  final host = await ref.watch(configsProvider.selectAsync((config) => config.host));

  return http.get('$host/products');
});

Implementation

AlwaysAliveProviderListenable<Future<Output>> selectAsync<Output>(
  Output Function(Input data) selector,
) {
  return _AlwaysAliveAsyncSelector(
    selector: selector,
    provider: this,
    future: future,
  );
}