Currently I am using a ListView.builder
to show items from a paginated API. Now I want to know which all items are visible on the screen? So that I can run certain logic for invisible items e.g unsubscribe live socket data for invisible items.
Why don’t you use stateful widgets (or watch_it) for your items? so you can use the initState and dispose function to do the subscribe / unsubscribe
That’s actually a great idea! Let me try going that route.
Invisible items are generally disposed when they scroll far enough out of the viewport. Your stream unsubscribe should be tied to dispose() for those widgets.
Thanks for this advice! I implemented this approach and it worked great. But the only problem is, when I navigate back to another screen. Since I use context.read() in the dispose to make the socket call. The context is not available after sometime for obvious reasons.
So I applied the context.mounted
check but in this case some calls won’t happen. Off course I can solve it by making a batch call to socket with all those elements which are still subscribed when the screen is disposed.
But do you think we can solve it another way?
I recommend using a service locator like get_it to access your socket then you don’t have to bother if the context still exists.
Yeah that make sense! Since I use get_it(thanks for creating it ) I will try that route as well. Thanks again