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
valueis accessed, it registers dependencies. - Efficient Updates: Notifies listeners only if the value actually changes.
- Seamless Integration: Works with
watchEffect()and Flutter'sValueListenable.
- Inheritance
-
- Object
- ChangeNotifier
- ReactiveNotifier<
T> - Ref
- Implemented types
- Available extensions
Properties
- $ → T
-
Alias
.valueno 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<
Sets the new list length.List< , provided by the RefListExt extensionT> >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<
Adds an element to the end of the queue.Queue< , provided by the RefQueueExt extensionT> > -
add(
T value) → void -
Available on Ref<
Adds a value and notifies listeners.List< , provided by the RefListExt extensionT> > -
add(
T value) → bool -
Available on Ref<
Adds a single value to the set.Set< , provided by the RefSetExt extensionT> > -
addAll(
Iterable< T> elements) → void -
Available on Ref<
Adds all items inSet< , provided by the RefSetExt extensionT> >elements. -
addAll(
Iterable< T> values) → void -
Available on Ref<
Adds multiple elements.List< , provided by the RefListExt extensionT> > -
addAll(
Map< K, V> other) → void -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
addFirst(
T value) → void -
Available on Ref<
Adds an element to the front of the queue.Queue< , provided by the RefQueueExt extensionT> > -
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
clear(
) → void -
Available on Ref<
Clears all elements in the queue.Queue< , provided by the RefQueueExt extensionT> > -
clear(
) → void -
Available on Ref<
Clears the list.List< , provided by the RefListExt extensionT> > -
clear(
) → void -
Available on Ref<
Clears the entire set.Set< , provided by the RefSetExt extensionT> > -
clear(
) → void -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
containsKey(
K key) → bool -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
containsValue(
V value) → bool -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
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<
Fills a range with a value.List< , provided by the RefListExt extensionT> > -
insert(
int index, T element) → void -
Available on Ref<
Inserts an element at a given index.List< , provided by the RefListExt extensionT> > -
insertAll(
int index, Iterable< T> elements) → void -
Available on Ref<
Inserts all elements starting atList< , provided by the RefListExt extensionT> >index. -
mapInPlace(
T convert(T)) → void -
Available on Ref<
Maps every element usingSet< , provided by the RefSetExt extensionT> >convert, in-place. -
mapInPlace(
T convert(T element)) → void -
Available on Ref<
Maps each element in-place. Useful for transforms without creating a new list.List< , provided by the RefListExt extensionT> > -
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< , provided by the RefMapExt extensionK, V> > -
remove(
T value) → bool -
Available on Ref<
Removes the first matching value.List< , provided by the RefListExt extensionT> > -
remove(
K key) → V? -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
remove(
T value) → bool -
Available on Ref<
Removes a single value.Set< , provided by the RefSetExt extensionT> > -
removeAll(
Iterable< T> elements) → void -
Available on Ref<
Removes all items inSet< , provided by the RefSetExt extensionT> >elements. -
removeAt(
int index) → T -
Available on Ref<
Removes element at an index.List< , provided by the RefListExt extensionT> > -
removeFirst(
) → T -
Available on Ref<
Removes and returns the first element.Queue< , provided by the RefQueueExt extensionT> > -
removeLast(
) → T -
Available on Ref<
Removes and returns the last element.List< , provided by the RefListExt extensionT> > -
removeLast(
) → T -
Available on Ref<
Removes and returns the last element.Queue< , provided by the RefQueueExt extensionT> > -
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<
Removes a range of elements.List< , provided by the RefListExt extensionT> > -
removeWhere(
bool test(T element)) → void -
Available on Ref<
Removes every element that satisfiesSet< , provided by the RefSetExt extensionT> >test. -
removeWhere(
bool test(T element)) → void -
Available on Ref<
Removes elements that match the predicate.List< , provided by the RefListExt extensionT> > -
removeWhere(
bool test(K key, V value)) → void -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
replaceRange(
int start, int end, Iterable< T> replacement) → void -
Available on Ref<
Replaces a range with new elements.List< , provided by the RefListExt extensionT> > -
retainAll(
Iterable< T> elements) → void -
Available on Ref<
Retains only elements that exist inSet< , provided by the RefSetExt extensionT> >elements. -
retainWhere(
bool test(T element)) → void -
Available on Ref<
Keeps only elements that satisfySet< , provided by the RefSetExt extensionT> >test. -
retainWhere(
bool test(T element)) → void -
Available on Ref<
Keeps only elements that satisfyList< , provided by the RefListExt extensionT> >test. -
reverse(
) → void -
Available on Ref<
Replaces the list with its reversed order.List< , provided by the RefListExt extensionT> > -
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<
Sets value at index (identical toList< , provided by the RefListExt extensionT> >operator[]=). -
setRange(
int start, int end, Iterable< T> source, [int skipCount = 0]) → void -
Available on Ref<
Sets a range of values fromList< , provided by the RefListExt extensionT> >source. -
shuffle(
[Random? random]) → void -
Available on Ref<
Randomly shuffles the list.List< , provided by the RefListExt extensionT> > -
sort(
[int compare(T a, T b)?]) → void -
Available on Ref<
Sorts the list in-place.List< , provided by the RefListExt extensionT> > -
toggle(
T value) → void -
Available on Ref<
Toggles presence ofSet< , provided by the RefSetExt extensionT> >value. -
toggle(
K key, V buildValue()) → void -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
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< , provided by the RefMapExt extensionK, V> > -
updateAllValues(
V updater(K key, V value)) → void -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
updateValue(
K key, V updater(V old)) → void -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> >
Operators
-
operator +(
T value) → void -
Available on Ref<
Adds a single value to the end of the list.List< , provided by the RefListExt extensionT> > -
operator +(
MapEntry< K, V> entry) → void -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
operator +(
T value) → void -
Available on Ref<
AddsSet< , provided by the RefSetExt extensionT> >valueto the set. -
operator -(
T value) → void -
Available on Ref<
Removes the first matching value from the list.List< , provided by the RefListExt extensionT> > -
operator -(
K key) → void -
Available on Ref<
Map< , provided by the RefMapExt extensionK, V> > -
operator -(
T value) → void -
Available on Ref<
RemovesSet< , provided by the RefSetExt extensionT> >valuefrom the set. -
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator [](
int index) → T? -
Available on Ref<
Returns the element at the given index.List< , provided by the RefListExt extensionT> > -
operator [](
K key) → V? -
Available on Ref<
Get value like normalMap< , provided by the RefMapExt extensionK, V> > -
operator []=(
int index, T value) → void -
Available on Ref<
Sets the element at the given index.List< , provided by the RefListExt extensionT> > -
operator []=(
K key, V value) → void -
Available on Ref<
Set value for keyMap< , provided by the RefMapExt extensionK, V> >