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

Listen to Changes from a Notifier #854

Open
PeterAkande opened this issue Jan 1, 2024 · 3 comments
Open

Listen to Changes from a Notifier #854

PeterAkande opened this issue Jan 1, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@PeterAkande
Copy link

Just as Its easy to add a listener callback to the BlocConsumer in flutter bloc, It would be great is there is such callback In the Consumers and Selectors provided by Provider.

For example, a Consumer with a Listener can be defined as:

Consumer<SomeState>(
    listener: (state){},
    builder: (context, state, child){
        return SomeWidget();
     },
);
@rrousselGit
Copy link
Owner

This had been requested before.
I haven't found a graceful way to do it quite yet.

Bloc does it because it has Bloc.listen. But there's no equivalent for providers.
The closest is State.didChangeDependencies, but it can be invoked for reasons other than a specific dependency change.

@PeterAkande
Copy link
Author

Did this as a work around that.

What do you think about it?

class ConsumerWithListener<T> extends StatefulWidget {
  final Widget? child;
  final void Function(T value) listener;
  final Widget Function(
    BuildContext context,
    T value,
    Widget? child,
  ) builder;

  const ConsumerWithListener({
    super.key,
    this.child,
    required this.listener,
    required this.builder,
  });

  @override
  State<ConsumerWithListener<T>> createState() =>
      _ConsumerWithListenerState<T>();
}

class _ConsumerWithListenerState<T> extends State<ConsumerWithListener<T>> {
  Timer? debounceTimer;

  @override
  Widget build(BuildContext context) {
    return Consumer<T>(
      builder: (context, T value, child) {
        if (debounceTimer?.isActive ?? false) debounceTimer?.cancel();

        /// The build Function can be called multiple Times for each notification
        /// But we don't want our listener to be called so many times
        /// So lets Debounce some unneeded calls.
        /// This is especially needed in cases where A Navigator.push is done in the
        /// Listen function.
        ///
        /// An error that is due to assert(!_debugLock) is thrown.
        debounceTimer = Timer(const Duration(milliseconds: 100), () {
          widget.listener.call(value);
        });

        return widget.builder.call(context, value, child);
      },
      child: widget.child,
    );
  }
}

souravbapari1 pushed a commit to souravbapari1/provider that referenced this issue Jan 28, 2024
@souravbapari1
Copy link

souravbapari1 commented Jan 28, 2024

i think this feature is helpful, OK i Add This

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

No branches or pull requests

3 participants