Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ValueStreamListener and ValueStreamConsumer widgets #779

Closed
GiancarloCante opened this issue Jan 27, 2025 · 2 comments · Fixed by #782
Closed

Add ValueStreamListener and ValueStreamConsumer widgets #779

GiancarloCante opened this issue Jan 27, 2025 · 2 comments · Fixed by #782
Assignees

Comments

@GiancarloCante
Copy link
Contributor

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!

@hoc081098
Copy link
Collaborator

PRs are welcome 🙏

@hoc081098
Copy link
Collaborator

I think keeping ValueStreamBuilder makes sense because it provides a simple and straightforward way to work with streams in Flutter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants