Ref<T> class

A reactive container that holds a single value of type T and provides notifications whenever the value changes.

This class implements ValueListenable to notify listeners when the value updates. It also extends ReactiveNotifier to integrate with the reactive ecosystem.

Example Usage:

// Create a reactive reference with an initial value
final count = Ref<int>(0);

// Listen for changes using a ValueListenableBuilder
ValueListenableBuilder<int>(
  valueListenable: count,
  builder: (context, value, child) {
    return Text('Current Count: $value');
  },
);

// Update the value and trigger notifications
count.value += 1; // UI updates automatically

// Reactivity: A dependent function can automatically respond to changes
watchEffect(() {
  print('Count changed: ${count.value}');
});

Features:

  • Reactive State: When value is accessed, it registers dependencies.
  • Efficient Updates: Notifies listeners only if the value actually changes.
  • Seamless Integration: Works with watchEffect() and Flutter's ValueListenable.
Inheritance
Implemented types
Available extensions

Constructors

Ref(T _value)
Initializes a new Ref with the given initial value.

Properties

$ → T
Alias .value
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
length int

Available on Ref<List<T>>, provided by the RefListExt extension

Sets the new list length.
no getter
mounted bool
Whether the notifier is mounted.
no setterinherited
notifyChange VoidCallback
latefinalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
value ↔ T
The current value of this Ref.
getter/setter pairoverride-getter

Methods

add(T value) → void

Available on Ref<Queue<T>>, provided by the RefQueueExt extension

Adds an element to the end of the queue.
add(T value) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Adds a value and notifies listeners.
add(T value) bool

Available on Ref<Set<T>>, provided by the RefSetExt extension

Adds a single value to the set.
addAll(Iterable<T> elements) → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Adds all items in elements.
addAll(Iterable<T> values) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Adds multiple elements.
addAll(Map<K, V> other) → void

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

addFirst(T value) → void

Available on Ref<Queue<T>>, provided by the RefQueueExt extension

Adds an element to the front of the queue.
addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
clear() → void

Available on Ref<Queue<T>>, provided by the RefQueueExt extension

Clears all elements in the queue.
clear() → void

Available on Ref<List<T>>, provided by the RefListExt extension

Clears the list.
clear() → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Clears the entire set.
clear() → void

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

containsKey(K key) bool

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

containsValue(V value) bool

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
inherited
fillRange(int start, int end, T value) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Fills a range with a value.
insert(int index, T element) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Inserts an element at a given index.
insertAll(int index, Iterable<T> elements) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Inserts all elements starting at index.
mapInPlace(T convert(T)) → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Maps every element using convert, in-place.
mapInPlace(T convert(T element)) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Maps each element in-place. Useful for transforms without creating a new list.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
putIfAbsent(K key, V value()) → V

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

remove(T value) bool

Available on Ref<List<T>>, provided by the RefListExt extension

Removes the first matching value.
remove(K key) → V?

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

remove(T value) bool

Available on Ref<Set<T>>, provided by the RefSetExt extension

Removes a single value.
removeAll(Iterable<T> elements) → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Removes all items in elements.
removeAt(int index) → T

Available on Ref<List<T>>, provided by the RefListExt extension

Removes element at an index.
removeFirst() → T

Available on Ref<Queue<T>>, provided by the RefQueueExt extension

Removes and returns the first element.
removeLast() → T

Available on Ref<List<T>>, provided by the RefListExt extension

Removes and returns the last element.
removeLast() → T

Available on Ref<Queue<T>>, provided by the RefQueueExt extension

Removes and returns the last element.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
removeRange(int start, int end) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Removes a range of elements.
removeWhere(bool test(T element)) → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Removes every element that satisfies test.
removeWhere(bool test(T element)) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Removes elements that match the predicate.
removeWhere(bool test(K key, V value)) → void

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

replaceRange(int start, int end, Iterable<T> replacement) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Replaces a range with new elements.
retainAll(Iterable<T> elements) → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Retains only elements that exist in elements.
retainWhere(bool test(T element)) → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Keeps only elements that satisfy test.
retainWhere(bool test(T element)) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Keeps only elements that satisfy test.
reverse() → void

Available on Ref<List<T>>, provided by the RefListExt extension

Replaces the list with its reversed order.
select<U>(U getter(T value)) Computed<U>
Creates a Computed that selects a value from the notifier.
inherited
setAt(int index, T value) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Sets value at index (identical to operator[]=).
setRange(int start, int end, Iterable<T> source, [int skipCount = 0]) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Sets a range of values from source.
shuffle([Random? random]) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Randomly shuffles the list.
sort([int compare(T a, T b)?]) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Sorts the list in-place.
toggle(T value) → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Toggles presence of value.
toggle(K key, V buildValue()) → void

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

toString() String
Returns a string representation containing the runtime type and the current value.
override
update(K key, V update(V value), {V ifAbsent()?}) → V

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

updateAllValues(V updater(K key, V value)) → void

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

updateValue(K key, V updater(V old)) → void

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

Operators

operator +(T value) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Adds a single value to the end of the list.
operator +(MapEntry<K, V> entry) → void

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

operator +(T value) → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Adds value to the set.
operator -(T value) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Removes the first matching value from the list.
operator -(K key) → void

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

operator -(T value) → void

Available on Ref<Set<T>>, provided by the RefSetExt extension

Removes value from the set.
operator ==(Object other) bool
The equality operator.
inherited
operator [](int index) → T?

Available on Ref<List<T>>, provided by the RefListExt extension

Returns the element at the given index.
operator [](K key) → V?

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

Get value like normal
operator []=(int index, T value) → void

Available on Ref<List<T>>, provided by the RefListExt extension

Sets the element at the given index.
operator []=(K key, V value) → void

Available on Ref<Map<K, V>>, provided by the RefMapExt extension

Set value for key