where method
where allows you to set a filter on a ValueListenable
so that an installed
handler function is only called if the passed
selector
function returns true. Because the selector function is called on
every new value you can change the filter during runtime.
ATTENTION: Due to the nature of ValueListeners that they always have to have
a value the filter can't work on the initial value. Therefore it's not
advised to use where inside the Widget tree if you use setState
because that
will recreate the underlying WhereValueNotifier
again passing through the lates
value of the this
even if it doesn't fulfill the selector
condition.
Therefore it's better not to use it directly in the Widget tree but in
your state objects
example: lets only print even values
final sourceListenable = ValueNotifier<int>(0);
final subscription = sourceListenable.where( (x)=>x.isEven )
.listen( (s,_) => print(x) );
Implementation
ValueListenable<T> where(bool Function(T) selector) {
return WhereValueNotifier(this.value, this, selector);
}