combineLatest<TIn2, TOut> method
ValueListenable<TOut>
combineLatest<TIn2, TOut>(
- ValueListenable<
TIn2> combineWith, - CombiningFunction2<
T, TIn2, TOut> combiner
Imagine having two ValueNotifier
in you model and you want to update
a certain region of the screen with their values every time one of them
get updated.
combineLatest combines two ValueListenable
in that way that it returns
a new ValueNotifier
that changes its value of TOut
whenever one of the
input listenables this
or combineWith
updates its value. This new value
is built by the combiner
function that is called on any value change of
the input listenables.
example:
class StringIntWrapper {
final String s;
final int i;
StringIntWrapper(this.s, this.i);
@override
String toString() {
return '$s:$i';
}
}
final listenable1 = ValueNotifier<int>(0);
final listenable2 = ValueNotifier<String>('Start');
final destValues = <StringIntWrapper>[];
final subscription = listenable1
.combineLatest<String, StringIntWrapper>(
listenable2, (i, s) => StringIntWrapper(s, i))
.listen((x, _) {
destValues.add(x);
});
listenable1.value = 42;
listenable1.value = 43;
listenable2.value = 'First';
listenable1.value = 45;
expect(destValues[0].toString(), 'Start:42');
expect(destValues[1].toString(), 'Start:43');
expect(destValues[2].toString(), 'First:43');
expect(destValues[3].toString(), 'First:45');
Implementation
ValueListenable<TOut> combineLatest<TIn2, TOut>(
ValueListenable<TIn2> combineWith,
CombiningFunction2<T, TIn2, TOut> combiner,
) {
return CombiningValueNotifier<T, TIn2, TOut>(
combiner(this.value, combineWith.value),
this,
combineWith,
combiner,
);
}