-
-
Notifications
You must be signed in to change notification settings - Fork 514
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
Labels
enhancement
New feature or request
Comments
This had been requested before. Bloc does it because it has |
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
i think this feature is helpful, OK i Add This |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
The text was updated successfully, but these errors were encountered: