LogoSignals.dart
Copy Markdown
rodydavis/signals.dart 999999

SetSignalMixin

A mixin that adds reactive <code>Set</code> methods and operations to a Signal.

A mixin that adds reactive Set methods and operations to a Signal holding a Set value.

This mixin delegates all standard Set operations (such as mutations like add, remove, addAll, removeAll, retainAll, and clear) to the underlying set, while ensuring that any reads register a dependency and any mutations automatically trigger reactive updates.

This mixin only works with signals that have a value type extending **Set**.

Example Usage#

import 'package:signals/signals.dart';

class MySetSignal extends Signal<Set<int>>
    with IterableSignalMixin<int, Set<int>>, SetSignalMixin<int, Set<int>> {
  MySetSignal(super.internalValue);
}

void main() {
  final numbers = MySetSignal({1, 2, 3});

  effect(() {
    print('Elements: $numbers, Length: ${numbers.length}');
  }); // Prints: "Elements: {1, 2, 3}, Length: 3"

  // Adding an element (automatically calls set() and triggers updates)
  numbers.add(4); // Prints: "Elements: {1, 2, 3, 4}, Length: 4"

  // Removing an element (triggers updates)
  numbers.remove(1); // Prints: "Elements: {2, 3, 4}, Length: 3"
}
Since mutations on SetSignalMixin notify listeners automatically, you do not need to assign numbers.value = ... to force updates. Methods like add, addAll, and remove take care of notification.

Methods#

View Methods
bool add(E value)
void addAll(Iterable<E> elements)
Set<R> cast()
void clear()
bool containsAll(Iterable<Object?> other)
Set<E> difference(Set<Object?> other)
Set<E> intersection(Set<Object?> other)
E? lookup(Object? object)
bool remove(Object? value)
void removeAll(Iterable<Object?> elements)
void removeWhere(bool Function(E element) test)
void retainAll(Iterable<Object?> elements)
void retainWhere(bool Function(E element) test)
Set<E> union(Set<E> other)