Skip to content

Add ValueStreamListener and ValueStreamConsumer widgets #779

Closed
@GiancarloCante

Description

@GiancarloCante

I would like to propose adding two new widgets to complement the existing ValueStreamBuilder:

  1. ValueStreamListener
    A widget that listens to a ValueStream and executes a callback when the stream emits new values, without rebuilding the widget tree. Useful for side effects like showing snackbars or navigation.

Signature:

ValueStreamListener<T>({
  required ValueStream<T> stream,
  required void Function(BuildContext context, T previous, T current) listener,
  required Widget child,
  Key? key,
})
  1. ValueStreamConsumer

A widget that combines both ValueStreamBuilder and ValueStreamListener functionality, allowing both rebuilding and side effects from a single widget.

Signature:

ValueStreamConsumer<T>({
  required ValueStream<T> stream,
  required Widget Function(BuildContext context, T value) builder,
  bool Function(T previous, T current)? buildWhen,
  void Function(BuildContext context, T previous, T current)? listener,
  Key? key,
})

Notes:

  • The builder callback uses value instead of data as its parameter name to better align with ValueStream's value-focused nature and its .value getter. This makes the API more intuitive when working with ValueStreams.
  • Unlike other similar implementations, the listener in ValueStreamConsumer is optional. This design choice allows the Consumer to be used primarily as a builder while maintaining the flexibility to add or remove listeners as needed, without requiring a complete widget swap.

I have implementations of these widgets that I've been using in my projects and would be happy to contribute them via PR in the next few days.

Let me know if you're interested in this contribution and if you'd like to see the PR.

Thanks!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions