SetNotifier<T> class

A Set that behaves like ValueNotifier if its data changes.

Notification Modes

  • CustomNotifierMode.normal: Listeners are only notified when a value actually changes. For example, adding an element that already exists in the set will not notify listeners. No-op operations (like removing a non-existent element) also won't notify.

  • CustomNotifierMode.always: Listeners are notified on every operation, even if the set doesn't change. This ensures UI always updates when operations are attempted.

  • CustomNotifierMode.manual: No automatic notifications. You must call notifyListeners manually after making changes.

Bulk Operations

Bulk operations like addAll, removeAll, and retainAll always notify listeners, even with empty input, regardless of notification mode (except manual). This ensures consistent behavior and prevents subtle bugs.

Transactions

Use startTransAction and endTransAction to batch multiple operations into a single notification. This is useful for atomic updates.

Set Operations

Set operations like union(), intersection(), and difference() return new sets and don't modify the current set, so they don't trigger notifications and are not overridden.

Note on Equality

Unlike ListNotifier and MapNotifier, SetNotifier does NOT support custom equality functions. Sets inherently use == and hashCode for membership testing, and custom equality would only apply to notification decisions, which could be confusing. Use the built-in equality behavior.

Examples

// Normal mode - only notifies on actual changes
final setNotifier = SetNotifier<int>(
  data: {1, 2, 3},
  notificationMode: CustomNotifierMode.normal,
);
setNotifier.add(1);  // No notification (already exists)
setNotifier.add(4);  // Notifies (new element added)
setNotifier.remove(99);  // No notification (doesn't exist)

// Always mode - notifies on every operation
final alwaysSet = SetNotifier<int>(
  data: {1, 2, 3},
  notificationMode: CustomNotifierMode.always,
);
alwaysSet.add(1);  // Notifies even though element exists
Implemented types
Mixed-in types
Available extensions

Constructors

SetNotifier({Set<T>? data, CustomNotifierMode notificationMode = CustomNotifierMode.always})
Creates a new listenable Set.

Properties

first → T
The first element.
no setterinherited
firstOrNull → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The first element of this iterator, or null if the iterable is empty.
no setter
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
indexed Iterable<(int, T)>

Available on Iterable<T>, provided by the IterableExtensions extension

Pairs of elements of the indices and elements of this iterable.
no setter
isEmpty bool
Whether this collection has no elements.
no setterinherited
isNotEmpty bool
Whether this collection has at least one element.
no setterinherited
iterator Iterator<T>
A new Iterator that allows iterating the elements of this Iterable.
no setterinherited
last → T
The last element.
no setterinherited
lastOrNull → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The last element of this iterable, or null if the iterable is empty.
no setter
length int
The number of elements in this Iterable.
no setterinherited
nonNulls Iterable<T>

Available on Iterable<T?>, provided by the NullableIterableExtensions extension

The non-null elements of this iterable.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
single → T
Checks that this iterable has only one element, and returns that element.
no setterinherited
singleOrNull → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The single element of this iterator, or null.
no setter
value Set<T>
Returns an immutable view of the current set state.
no setteroverride
wait Future<List<T>>

Available on Iterable<Future<T>>, provided by the FutureIterable extension

Waits for futures in parallel.
no setter

Methods

add(T value) bool
from here all functions are equal to Set<T> with the addition that all modifying functions will call notifyListener if not in a transaction.
addAll(Iterable<T> elements) → void
Adds all elements to this set.
addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
any(bool test(T)) bool
Checks whether any element of this iterable satisfies test.
inherited
asNameMap() Map<String, T>

Available on Iterable<T>, provided by the EnumByName extension

Creates a map from the names of enum values to the values.
byName(String name) → T

Available on Iterable<T>, provided by the EnumByName extension

Finds the enum value in this list with name name.
cast<T>() Set<T>
A view of this iterable as an iterable of R instances.
inherited
clear() → void
Removes all elements from the set.
contains(Object? element) bool
Whether the collection contains an element equal to element.
inherited
containsAll(Iterable<Object?> other) bool
Whether this set contains all the elements of other.
inherited
debounce(Duration timeOut) Listenable

Available on Listenable, provided by the FunctionaListener2 extension

difference(Set<Object?> other) Set<T>
Creates a new set with the elements of this that are not in other.
inherited
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
elementAt(int index) → T
Returns the indexth element.
inherited
elementAtOrNull(int index) → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The element at position index of this iterable, or null.
endTransAction() → void
Ends a transaction.
every(bool test(T)) bool
Checks whether every element of this iterable satisfies test.
inherited
expand<T>(Iterable<T> f(T)) Iterable<T>
Expands each element of this Iterable into zero or more elements.
inherited
firstWhere(bool test(T), {T orElse()?}) → T
The first element that satisfies the given predicate test.
inherited
fold<T>(T initialValue, T combine(T previousValue, T element)) → T
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value
inherited
followedBy(Iterable<T> other) Iterable<T>
Creates the lazy concatenation of this iterable and other.
inherited
forEach(void f(T)) → void
Invokes action on each element of this iterable in iteration order.
inherited
intersection(Set<Object?> other) Set<T>
Creates a new set which is the intersection between this set and other.
inherited
join([String separator = '']) String
Converts each element to a String and concatenates the strings.
inherited
lastWhere(bool test(T), {T orElse()?}) → T
The last element that satisfies the given predicate test.
inherited
listen(void handler(ListenableSubscription)) ListenableSubscription

Available on Listenable, provided by the FunctionaListener2 extension

let you work with a Listenable as it should be by installing a handler function that is called on any value change of this and gets the new value passed as an argument. It returns a subscription object that lets you stop the handler from being called by calling cancel() on the subscription. The handler get the subscription object passed on every call so that it is possible to uninstall the handler from the handler itself.
lookup(Object? element) → T?
If an object equal to object is in the set, return it.
inherited
map<T>(T f(T)) Iterable<T>
The current elements of this iterable modified by toElement.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
If needed you can notify all listeners manually.
override
reduce(T combine(T value, T element)) → T
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
inherited
remove(Object? value) bool
Removes value from the set.
removeAll(Iterable<Object?> elements) → void
Removes each element of elements from this set.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
removeWhere(bool test(T)) → void
Removes all elements of this set that satisfy test.
retainAll(Iterable<Object?> elements) → void
Removes all elements of this set that are not elements in elements.
retainWhere(bool test(T)) → void
Removes all elements of this set that fail to satisfy test.
retype<T>() Set<T>
inherited
singleWhere(bool test(T), {T orElse()?}) → T
The single element that satisfies test.
inherited
skip(int n) Iterable<T>
Creates an Iterable that provides all but the first count elements.
inherited
skipWhile(bool test(T)) Iterable<T>
Creates an Iterable that skips leading elements while test is satisfied.
inherited
startTransAction() → void
Starts a transaction that allows to make multiple changes to the Set with only one notification at the end.
take(int n) Iterable<T>
Creates a lazy iterable of the count first elements of this iterable.
inherited
takeWhile(bool test(T)) Iterable<T>
Creates a lazy iterable of the leading elements satisfying test.
inherited
toList({bool growable = true}) List<T>
Creates a List containing the elements of this Iterable.
inherited
toSet() Set<T>
Creates a Set containing the same elements as this iterable.
inherited
toString() String
A string representation of this object.
inherited
union(Set<T> other) Set<T>
Creates a new set which contains all the elements of this set and other.
inherited
where(bool test(T)) Iterable<T>
Creates a new lazy Iterable with all elements that satisfy the predicate test.
inherited
whereType<T>() Iterable<T>
Creates a new lazy Iterable with all elements that have type T.
inherited

Operators

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