collectAsState method
ValueNotifier<T>
collectAsState({
- T? initial,
- T onError(
- Object error,
- StackTrace stackTrace
- Cancellable? cancellable,
Collects the future as a ValueNotifier.
Implementation
ValueNotifier<T> collectAsState(
{T? initial,
T Function(Object error, StackTrace stackTrace)? onError,
Cancellable? cancellable}) {
ValueNotifier<T>? notifier;
ValueNotifier<T> createNotifier(T value) {
if (cancellable == null) {
return ValueNotifier<T>(value);
} else {
return CancellableValueNotifier<T>(value, cancellable);
}
}
if (initial != null) {
notifier = createNotifier(initial);
then((event) {
notifier!.value = event;
}, onError: (error, stackTrace) {
if (onError != null) {
notifier!.value = onError(error, stackTrace);
}
});
} else {
then((event) {
if (notifier == null) {
notifier = createNotifier(event);
} else {
notifier!.value = event;
}
}, onError: (error, stackTrace) {
if (onError != null) {
final value = onError(error, stackTrace);
if (notifier == null) {
notifier = createNotifier(value);
} else {
notifier!.value = value;
}
}
});
}
assert(notifier != null,
'Future is not syncFuture and no initial value was provided.');
return notifier as ValueNotifier<T>;
}