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

isExecuting and canExecute are constantly recreated #36

Closed
smiLLe opened this issue Jan 29, 2020 · 4 comments
Closed

isExecuting and canExecute are constantly recreated #36

smiLLe opened this issue Jan 29, 2020 · 4 comments

Comments

@smiLLe
Copy link

smiLLe commented Jan 29, 2020

Hi,

i was using flutter_hooks (but the problem will probably show for streambuilder, provider etc. as well) where i have a widget like this:

HookBuilder(builder: (context) {
  final snapshot = useStream(cmd.isExecuting);
  return Text('hello');
})

The problem is, useStream will listen to a stream until it receives a new stream, then it will unsub to the previous and listen to the new stream...
isExecuting is a BehaviorSubject and immediately pushes the latest value, which useStream will take and calls setState();
Now the HookBuilder will build again, cmd.isExecuting returns a new stream, useStream will listen to the new stream, gets the first value from BehaviorSubject and rebuilds the widget... and again and again.

I can solve this with cmd.isExecuting.skip(1) but wouldn't it be better to change

Stream<bool> get canExecute => _canExecuteSubject.startWith(true).distinct();

into

Stream<bool> _canExecute
Stream<bool> get canExecute => _canExecute ??= _canExecuteSubject.startWith(true).distinct();
@escamoteur
Copy link
Collaborator

Hi, that is indeed an interesting point. I change it and check if all is still working

@escamoteur
Copy link
Collaborator

Please give the new version 5.02 a test

@escamoteur
Copy link
Collaborator

Have you tried it?

@smiLLe
Copy link
Author

smiLLe commented Jan 31, 2020

Hi, the update is working :) no infinite rebuilds

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

No branches or pull requests

2 participants