Ref<T> class
A reactive container that holds a value and notifies on change.
Ref is the fundamental reactive primitive in Lark. It stores
a value of type T and automatically notifies any Computed or
Effect that reads it when the value changes.
Creating Refs
Use the ref factory function:
final count = ref(0);
final user = ref<User?>(null);
final items = ref<List<String>>([]);
Reading Values
// Standard getter (tracks dependencies)
print(count.value);
// Shorthand call syntax
print(count());
// Without tracking
print(count.peek());
Writing Values
// Direct assignment
count.value = 10;
// Update based on current value
count.update((n) => n + 1);
// Functional set (returns the ref for chaining)
count.set(0);
Best Practices
-
Keep refs granular: Prefer multiple small refs over one large object ref
-
Use update for mutations: For complex types, use update to safely modify based on current value
-
Consider custom equality: For collections or complex objects, provide an equality function to avoid unnecessary updates
- Implemented types
-
- ReadonlyRef<
T>
- ReadonlyRef<
Constructors
-
Ref(T _value, {EqualityFn<
T> ? equals, String? debugName}) - Creates a new ref with the given initial value.
Properties
- debugName → String?
-
Debug name for this ref.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasSubscribers → bool
-
Whether this ref has any subscribers.
no setter
- isDisposed → bool
-
Whether this ref has been disposed.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- subscriberCount → int
-
Number of active subscribers.
no setter
- value ↔ T
-
Gets the current value, tracking the dependency.
getter/setter pairoverride-getter
Methods
-
call(
) → T -
Shorthand for getting value.
override
-
dispose(
) → void - Disposes this ref, releasing all subscribers.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyAll(
) → void - Forces notification to all subscribers.
-
peek(
) → T -
Gets the current value without tracking the dependency.
override
-
set(
T newValue) → Ref< T> - Sets a new value and returns this ref for chaining.
-
toString(
) → String -
A string representation of this object.
override
-
update(
T updater(T current)) → void - Updates the value based on the current value.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited