getx
getx copied to clipboard
Inconsistent visibility for `value` of `RxList` / `RxSet`
What is the recommended way to update the value of an RxList
/ RxSet
, specifically when you want to reset the whole list (exchange every item).
There are 3 approaches possible:
final RxList<int> list = [1,2,3].obs;
list.value = [4,5]; // triggers warning
This triggers an analyzer warning, since the setter for value
is marked as @protected
.
final RxList<int> list = [1,2,3].obs;
list([4,5]); // equal to list.call([4,5]), triggers no warning
This does not trigger an analyzer warning, since the call
method is not marked as @protected
. However, this internally does call the setter for value
and has not rly another use. Therefore this looks like an oversight / backdoor, instead of a legitimate usage. Or is this inconsistency intended?
final RxList<int> list = [1,2,3].obs;
list.clear();
list.addAll([4,5]);
This operates on the list value
instead of replacing it. But this does trigger 2 operations and therefore presumably would trigger 2 times a new build of Obx
widgets that depend on the observable.
Summary:
It does not seem like 1) or 2) pose any problems with behavior of the observables or the Obx
widgets using them. Is there a particular reason why one should avoid setting the value
of RxList
or RxSet
directly? Is there a reason why the setter for value
is marked as @protected
? Are you supposed to instead modify the list in seperate steps or is there a more performant solution?
I'm also confused by this. Often, I need to update an entire ListView. However, using RxList inside an Obx will trigger an improper use of Obx. I often, just add list.value to avoid this error, but run into a warning due to the value being protected. I just suppress the warning...
I don't really see a reason for the @protected annotation.
I haven't tried this, but if it is not protected, maybe it's intended in case in future there are some additional code to run before an assignment. I guess to mitigate breaking changes?
I would not use this method personally as it seem to be doing much more work than necessary. But from testing, the framework is smart enough to not rebuild more than once if multiple observables have changed in a single call.
Any update?
You should use assignAll
or assign
yourRxList.assignAll(yourNewList) yourRxList.assign(yourNewObject)
https://github.com/jonataslaw/getx/blob/master/documentation/en_US/state_management.md#note-about-lists