nullableFuture<T> method
Consumes a nullable Future and watches the given future
.
Implemented by calling Future.asStream and forwarding calls onto nullableStream.
If the given future changes, then the current StreamSubscription
will be disposed and recreated for the new future.
Thus, it is important that the future instance only changes when needed.
It is incorrect to create a future in the same build as the future
,
unless you use something like memo to limit changes.
Or, if possible, it is even better to wrap the future in an entirely
new capsule (although this is not always possible).
This side effect also caches the data from the latest non-null future
,
passing it into AsyncLoading.previousData when the future switches and
AsyncError.previousData when a new future throws an exception.
To remove this cached data from the returned AsyncValue,
you may call AsyncValueConvenience.withoutPreviousData.
Implementation
AsyncValue<T>? nullableFuture<T>(Future<T>? future) {
// NOTE: we convert to a stream here to reuse our cancellation code.
final asNullableStream = use.memo(() => future?.asStream(), [future]);
return use.nullableStream(asNullableStream);
}