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

Stream ending with BadState when navigating back and forth quickly #3485

Open
AxelBlaz3 opened this issue Apr 10, 2024 · 8 comments
Open

Stream ending with BadState when navigating back and forth quickly #3485

AxelBlaz3 opened this issue Apr 10, 2024 · 8 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@AxelBlaz3
Copy link

Describe the bug
I have an app where there is a button in the home page. Upon clicking the button, it navigates me to accounts list page which are retrieved from a local database query stream. When I go to accounts page and come back, everything works fine. However, if I immediately go to accounts page as soon as I am back to home page, it throws me BadState: Stream has already been listened to error. Upon investigating, I found that it takes about a second to cleanup the provider state. Meaning, if I navigate do not navigate quickly to accounts page from home, it works fine.

I tried it with two different databases - Isar and ObjectBox. Both of them were having the same behaviour. If I remove the provider and manually get the stream, it works fine because the widget lifecycle disposes of the stream as soon as I go back (pop the navigation stack).

To Reproduce
I am attaching a sample project with instructions on the home page to reproduce the bug.
https://github.com/AxelBlaz3/Riverpod-StreamBuilder-Bug

Expected behavior
Riverpod should clean up the provider state immediately after I pop the current page because there are no listeners after popping the current page. However, it takes about a second to clean things up which is not what I am expecting (but correct me if I am mistaken).

@AxelBlaz3 AxelBlaz3 added bug Something isn't working needs triage labels Apr 10, 2024
@AxelBlaz3
Copy link
Author

I am attaching the demo video. If you look carefully you will see that I get a bad stream error only when I quickly navigate to accounts page from home page (I got to home page form accounts page by back gesture on my device). It works fine on normal navigation speed.

file.mp4

Just as a note, I have AccountsUiState class with only Stream<List<Account>> because I wanted it to be minimal. In real project, I have more than just a stream.

@RandalSchwartz
Copy link

Just filling in, it looks like the stream is being listened twice, which a singleuser stream cannot. But the puzzle is why doesn't the provider fully recycle during the quick back and forth, which would have created a new stream?

@AxelBlaz3
Copy link
Author

AxelBlaz3 commented Apr 11, 2024

@RandalSchwartz exactly! I am curious why the provider does not fully recycle during quick back and forth.

@rrousselGit
Copy link
Owner

There's too much code here. I can't realistically investigate a whole project. Please create a minimal reproducible example.

But my guess is, this is most likely a mistake on your end. I'd suggest asking for help on Discord

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels Apr 11, 2024
@AxelBlaz3
Copy link
Author

AxelBlaz3 commented Apr 11, 2024

@rrousselGit thanks for letting me know about it. I apologize for having a bit more code for a minimal sample. I hope the new code changes are simple to catch and understand. Have a look at it at your convenience.

Also, I have tried asking for help on Discord before I filed an issue. It could be a mistake from my end but I can't figure out where I am going wrong with this.

@AxelBlaz3
Copy link
Author

As a follow-up, I noticed that when I converted AccountsPage to ConsumerStatefulWidget and logged initState and dispose, dispose is being called with a delay when I navigate back.

@AxelBlaz3
Copy link
Author

I recently pushed a new branch called working, which works flawlessly and does not use riverpod to inject dependencies. It works fine regardless of how quick I navigate. The dispose() delay seems to be normal and it is how flutter handles internally from my observation.

As a note, the file providers.dart in working branch has Futures for global state.

@AxelBlaz3
Copy link
Author

It seems like I have to await for my stream in notifier's build() and then yield the List<Account>. This solves the problem but curious why using a StreamBuilder is not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants