ValueStreamConsumer<M, T> constructor

const ValueStreamConsumer<M, T>({
  1. Key? key,
  2. required ValueStream<M> stream,
  3. T? distinctBy(
    1. M event
    )?,
  4. required ValueStreamWidgetListener<M, T> listener,
  5. required ValueStreamWidgetBuilder<M> builder,
  6. ValueStreamBuilderCondition<M, T>? buildWhen,
  7. Widget? child,
  8. bool isReplayValueStream = true,
})

ValueStreamConsumer exposes a builder and listener to react to new values from a stream.

ValueStreamConsumer is analogous to a nested ValueStreamListener and ValueStreamBuilder but reduces the amount of boilerplate needed.

ValueStreamConsumer requires stream.hasValue to always be true, and the stream does not emit any error events. See ValueStreamHasNoValueError and UnhandledStreamError for more information.

ValueStreamConsumer should only be used when it is necessary to both rebuild UI and execute other reactions to value changes in the stream.

ValueStreamConsumer takes a required ValueStream, ValueStreamWidgetListener and ValueStreamWidgetBuilder and an optional ValueStreamBuilderCondition.

Example

ValueStreamConsumer<T>(
  stream: valueStream,
  listener: (context, previous, current) {
    // do stuff here based on valueStream's
    // previous and current values
  },
  builder: (context, value, child) {
    // Build widget based on valueStream's value
  },
  child: const SizedBox(), // Optional child widget that remains stable
)

An optional buildWhen can be implemented for more granular control over when builder is called. The buildWhen will be invoked on each stream value change. It takes the previous value and current value and must return a bool which determines whether or not the builder function will be invoked. The previous value will be initialized to the value of the stream when the ValueStreamConsumer is initialized. buildWhen is optional and if it isn't implemented, it will default to true.

child is optional but is good practice to use if part of the widget subtree does not depend on the value of the stream.

Example

ValueStreamConsumer<T>(
  stream: valueStream,
  listener: (context, previous, current) {
    // do stuff here based on valueStream's
    // previous and current values
  },
  buildWhen: (previous, current) {
    // return true/false to determine whether or not
    // to rebuild the widget with valueStream's value
  },
  builder: (context, value, child) {
    // Build widget based on valueStream's value
  },
  child: const SizedBox(), // Optional child widget that remains stable
)

Implementation

const ValueStreamConsumer({
  super.key,
  required this.stream,
  this.distinctBy,
  required this.listener,
  required this.builder,
  this.buildWhen,
  this.child,
  this.isReplayValueStream = true,
});