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

Right place for ref.listen() in ConsumerStatefulWidget #915

Closed
topperspal opened this issue Nov 11, 2021 · 7 comments
Closed

Right place for ref.listen() in ConsumerStatefulWidget #915

topperspal opened this issue Nov 11, 2021 · 7 comments
Labels
documentation Improvements or additions to documentation

Comments

@topperspal
Copy link

This time, the "ref" isn't passed as parameter of the build method, but is rather a property of the ConsumerState object:

But when I use ref.listen() in initState() it throws the following error :-

ref.listen can only be used in build method of ConsumerState

The question is that if I cannot use ref.listen() in init state then where I should place it in the class.

@topperspal topperspal added documentation Improvements or additions to documentation needs triage labels Nov 11, 2021
@daniellampl
Copy link

Place it at the beginning of the build method. It won't force your widget to rebuild if the provider updates its state. It just executes the callback you pass into it.

@topperspal
Copy link
Author

topperspal commented Nov 12, 2021 via email

@daniellampl
Copy link

ConsumerWidget actually takes care of closing previously opened provider listening subscriptions before it subscribes to them again. So when the widget rebuilds, the subscriptions get closed and resubscribed automatically.

@rrousselGit this should probably get mentioned in the documentation. What do you think?

@rrousselGit
Copy link
Owner

Closing in favor of #1370 and #1034. The former should allow initState to listen to providers, while the later will catch bugs where ref.listen is misused.

@ciriousjoker
Copy link

ciriousjoker commented Dec 6, 2023

@rrousselGit

What's the state of this? The docs still warn against using listen in initState, but there's no reason given. The linter you mentioned also doesn't complain about using listen in initState.

Are we supposed to use listen in initState or build?

Use case: provider contains a bool for muting audio. When the provider value changes, we want to update the widget's audiocontroller with the new volume.

Sorry for pinging, I just checked the dartDoc for listen and it explains everything.

@feronetick
Copy link

@rrousselGit

What's the state of this? The docs still warn against using listen in initState, but there's no reason given. The linter you mentioned also doesn't complain about using listen in initState.

Are we supposed to use listen in initState or build?

Use case: provider contains a bool for muting audio. When the provider value changes, we want to update the widget's audiocontroller with the new volume.

If you want to listen provider changes in initState you can use ref.listenManual method with fireImmediately flag

@ciriousjoker
Copy link

ciriousjoker commented Dec 6, 2023

Ah ok, so I guess for simple use cases like this, listenManual is the way to go. Unsubscribing & recreating the subscription seems like a waste to me even if it's done automatically.

Thanks a lot!

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

No branches or pull requests

5 participants