SelectorFor

Single Field Selector

SelectorFor allows to select specific fields in a object and trigger a callback when those fields change (by reference or by value).

SelectorFor is used together with BlocSelector , to choose the data from a Bloc state that we want to use as state that should trigger recomposition.

More precisely, with SelectorFor we can build an AbstractSelector object that we can pass as argument to BlocSelector. Let's see some examples on how this is done.

Let us assume that the state object from which we want to select data is the following:

class StateABC(val a:Int, val b:Int, val c:Int)

For example if we are interested in the a field, we can write:

//sel_a is of type AbstractSelector<StateABC>
val sel_a = SelectorFor<StateABC>.withSingleField { a }

Selector for Multiple Fields

With a selector we can also check for changes of multiple fields in an object and combine them in a computed value:

//sel_sum_ab is of type AbstractSelector<StateABC>
val sel_sum_ab =
 SelectorFor<StateABC>
    .withField{a}
    .withField{b}
    .compute {a,b -> a+b}

sel_sum_ab will keep track when the class field a and b change and automatically recompute their sum and trigger a state change with the value of the sum.

Equality Check by Reference or by Value

Let's say now that we the following state object:

Again, if we are interested in the a field, we can write

But what happens when the state change? The selector sel_astr we have built is able to identify changes to the astr field. By default changes are checked by reference . In other words if we have the two states:

the two states state1 and state2 are considered different by the selector sel_astr . This is not always the desired behaviour. Sometimes we want to check if the "value", not the "reference" of a field has changed. If this is what we want then we must use withSingleFieldByValue

With this definition of the selector state1 and state2 above will be considered equal and will not considered a state change.

We can choose equality check by value also for selectors based on multiple fields:

Building multiple selectors for the same state

SelectorFor is can be used to build multiple AbstractSelector at once, as we can see in the following example:

Last updated

Was this helpful?