fluttercommunity/rx_command

isExecuting and canExecute are constantly recreated

smiLLe opened this issue · 4 comments

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();

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

Please give the new version 5.02 a test

Have you tried it?

Hi, the update is working :) no infinite rebuilds