mergeAsyncValues<T, A, B> function
- AsyncValue<
A> a, - AsyncValue<
B> b, - MergeAsyncValueCallback<
T, A, B> combine
Merge two AsyncValues together, combining them into one value if both have data.
If both values have data, the combine function will be used to combine
both of them together. Both values need to have data for them to be
combined. If only one of them has a value, the resulting value will still
be a loading state.
Any errors will also appear in the resulting value's error. If only one value has an error, then the error will be included in the resulting async value. If both have errors, then a CombinedAsyncException will be returned in the resulting async value, with both errors present.
If only one of the given arguments has a value, or neither has a value, then the result will be a loading value.
Implementation
AsyncValue<T> mergeAsyncValues<T, A, B>(
AsyncValue<A> a,
AsyncValue<B> b,
MergeAsyncValueCallback<T, A, B> combine,
) {
if (a.hasValue && b.hasValue) {
final value = combine(a.value as A, b.value as B);
final asyncValue = AsyncValue.data(value);
if (a.hasError || b.hasError) {
return _mergeAsyncErrors<T, A, B>(a, b).copyWithPrevious(asyncValue);
}
if (a.isLoading || b.isLoading) {
return AsyncValue<T>.loading().copyWithPrevious(asyncValue);
}
return asyncValue;
}
if (a.hasError || b.hasError) {
return _mergeAsyncErrors<T, A, B>(a, b);
}
return AsyncValue<T>.loading();
}