copyWithPrevious method
Clone an AsyncValue, merging it with previous
.
When doing so, the resulting AsyncValue can contain the information about multiple state at once. For example, this allows an AsyncError to contain a value, or even AsyncLoading to contain both a value and an error.
The optional isRefresh
flag (true by default) represents whether the
provider rebuilt by Ref.refresh/Ref.invalidate (if true)
or instead by Ref.watch (if false).
This changes the default behavior of when and sets the isReloading
/
isRefreshing
flags accordingly.
Implementation
@override
AsyncValue<T> copyWithPrevious(
AsyncValue<T> previous, {
bool isRefresh = true,
}) {
if (isRefresh) {
return previous.map(
data: (d) => AsyncData._(
d.value,
isLoading: true,
error: d.error,
stackTrace: d.stackTrace,
),
error: (e) => AsyncError._(
e.error,
isLoading: true,
value: e.valueOrNull,
stackTrace: e.stackTrace,
hasValue: e.hasValue,
),
loading: (_) => this,
);
} else {
return previous.map(
data: (d) => AsyncLoading._(
hasValue: true,
value: d.valueOrNull,
error: d.error,
stackTrace: d.stackTrace,
),
error: (e) => AsyncLoading._(
hasValue: e.hasValue,
value: e.valueOrNull,
error: e.error,
stackTrace: e.stackTrace,
),
loading: (e) => e,
);
}
}