FutureSignal<T> class

Future signals can be created by extension or method.

futureSignal

final s = futureSignal(() async => 1);

toSignal()

final s = Future(() => 1).toSignal();

.value, .peek()

Returns AsyncState<T> for the value and can handle the various states.

The value getter returns the value of the future if it completed successfully.

.peek() can also be used to not subscribe in an effect

final s = futureSignal(() => Future(() => 1));
final value = s.value.value; // 1 or null

.reset()

The reset method resets the future to its initial state to recall on the next evaluation.

final s = futureSignal(() => Future(() => 1));
s.reset();

.refresh()

Refresh the future value by setting isLoading to true, but maintain the current state (AsyncData, AsyncLoading, AsyncError).

final s = futureSignal(() => Future(() => 1));
s.refresh();
print(s.value.isLoading); // true

.reload()

Reload the future value by setting the state to AsyncLoading and pass in the value or error as data.

final s = futureSignal(() => Future(() => 1));
s.reload();
print(s.value is AsyncLoading); // true

Dependencies

By default the callback will be called once and the future will be cached unless a signal is read in the callback.

final count = signal(0);
final s = futureSignal(() async => count.value);

await s.future; // 0
count.value = 1;
await s.future; // 1

If there are signals that need to be tracked across an async gap then use the dependencies when creating the futureSignal to reset every time any signal in the dependency array changes.

final count = signal(0);
final s = futureSignal(
    () async => count.value,
    dependencies: [count],
);
s.value; // state with count 0
count.value = 1; // resets the future
s.value; // state with count 1

@link https://dartsignals.dev/async/future

Inheritance

Constructors

FutureSignal.new(Future<T> fn(), {T? initialValue, String? debugLabel, List<ReadonlySignal> dependencies = const [], bool lazy = true, bool autoDispose = false})
Future signals can be created by extension or method.

Properties

autoDispose bool
Throws and error if read after dispose and can be disposed on last unsubscribe.
getter/setter pairinherited
cancelOnError bool?
Cancel the subscription on error
finalinherited
debugLabel String?
Debug label for Debug Mode
finalinherited
dependencies List<ReadonlySignal>
List of dependencies to recompute the stream
finalinherited
disposed bool
Check if the effect is disposed
getter/setter pairinherited
equalityCheck bool Function(AsyncState<T> a, AsyncState<T> b)
Optional method to check if to values are the same
getter/setter pairinherited
first Future<T>
Last value of the stream
no setterinherited
future Future<T>
The future of the signal completer
no setterinherited
globalId int
Global ID of the signal
finalinherited
hashCode int
The hash code for this object.
no setterinherited
internalValue AsyncState<T>
no setterinherited
isCompleted bool
Returns true if the signal is completed an error or data
no setterinherited
isDone bool
Check if the signal is done
no setterinherited
isInitialized bool
Check if the value is set and not a lazy signal
no setterinherited
isPaused bool
Check if the subscription is paused
no setterinherited
last Future<T>
First value of the stream
no setterinherited
requireValue → T
Returns the value of the signal
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
value AsyncState<T>
Compute the current value
getter/setter pairinherited
version int
Version numbers should always be >= 0, because the special value -1 is used by Nodes to signify potentially unused but recyclable nodes.
getter/setter pairinherited

Methods

add(T event) → void
Adds a data event to the sink.
inherited
addError(Object error, [StackTrace? stackTrace]) → void
Adds an error to the sink.
inherited
afterCreate(AsyncState<T> val) → void
Internal hook for after a signal is created
inherited
beforeUpdate(AsyncState<T> val) → void
Internal hook for after a signal is updated
inherited
call() AsyncState<T>
Return the value when invoked
inherited
cancel() Future<void>
Cancel the subscription
inherited
close() → void
Closes the sink.
inherited
dispose() → void
Dispose the signal
inherited
execute(Stream<T> src) Future<void>
Execute the stream
inherited
get() AsyncState<T>
Helper method to get the current value
inherited
init() → void
Initialize the signal
inherited
internalRefresh() bool
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDispose(void cleanup()) EffectCleanup
Add a cleanup function to be called when the signal is disposed
inherited
overrideWith(AsyncState<T> val) Signal<AsyncState<T>>
Override the current signal with a new value as if it was created with it
inherited
pause([Future<void>? resume]) → void
Pause the subscription
inherited
peek() AsyncState<T>
In the rare instance that you have an effect that should write to another signal based on the previous value, but you don't want the effect to be subscribed to that signal, you can read a signals's previous value via signal.peek().
inherited
readonly() ReadonlySignal<AsyncState<T>>
Returns a readonly signal
inherited
refresh() Future<void>
Refresh the future
override
reload() Future<void>
Reload the future
override
reset([AsyncState<T>? value]) → void
Reset the signal to the initial value
inherited
resume() → void
Resume the subscription
inherited
set(AsyncState<T> val, {bool force = false}) bool
Set the current value by a method
inherited
setError(Object error, [StackTrace? stackTrace]) → void
Set the error with optional stackTrace to AsyncError
inherited
setLoading([AsyncState<T>? state]) → void
Set the loading state to AsyncLoading
inherited
setValue(T value) → void
Set the value to AsyncData
inherited
subscribe(void fn(AsyncState<T> value)) → void Function()
Subscribe to value changes with a dispose function
inherited
subscribeToNode(Node node) → void
inherited
toJson() → dynamic
Convert value to JSON
inherited
toString() String
A string representation of this object.
inherited
unsubscribeFromNode(Node node) → void
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited