LogoSignals.dart
Copy Markdown
rodydavis/signals.dart 999999

ListSignalMixin

A mixin that adds reactive <code>List</code> methods and operators to a Signal.

A mixin that adds reactive List methods and operators to a Signal holding a List value.

This mixin delegates all standard List operations (such as mutations like add, remove, insert, sort, and clear, and accessor operators like [] and []=) to the underlying list, 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 **List**.

Example Usage#

import 'package:signals/signals.dart';

class MyListSignal extends Signal<List<int>>
    with IterableSignalMixin<int, List<int>>, ListSignalMixin<int, List<int>> {
  MyListSignal(super.internalValue);
}

void main() {
  final numbers = MyListSignal([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"

  // Modifying an element by index (triggers updates)
  numbers[0] = 10; // Prints: "Elements: [10, 2, 3, 4], Length: 4"
}
Since mutations on ListSignalMixin notify listeners automatically, you do not need to assign numbers.value = ... to force updates. Methods like add, addAll, and operator []= take care of notification.

Methods#

View Methods
List<R> cast()
first(E val)
E last
last(E val)
length(int value)
List<E> +(List<E> other)
E [](int index)
void []=(int index, E value)
void add(E value)
void addAll(Iterable<E> iterable)
Map<int, E> asMap()
void clear()
Iterable<R> expand(Iterable<R> Function(E element) toElements)
void fillRange(int start, int end, [E? fillValue])
E firstWhere(bool Function(E element) test, {E Function()? orElse})
R fold(R initialValue, R Function(R previousValue, E element) combine)
Iterable<E> followedBy(Iterable<E> other)
Iterable<E> getRange(int start, int end)
int indexOf(E element, [int start = 0])
int indexWhere(bool Function(E element) test, [int start = 0])
void insert(int index, E element)
void insertAll(int index, Iterable<E> iterable)
int lastIndexOf(E element, [int? start])
int lastIndexWhere(bool Function(E element) test, [int? start])
bool remove(Object? value)
E removeAt(int index)
E removeLast()
void removeRange(int start, int end)
void removeWhere(bool Function(E element) test)
void replaceRange(int start, int end, Iterable<E> replacements)
void retainWhere(bool Function(E element) test)
Iterable<E> reversed
void setAll(int index, Iterable<E> iterable)
void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0])
void shuffle([Random? random])
void sort([int Function(E a, E b)? compare])
List<E> sorted([int Function(E a, E b)? compare])

Return a new array that is sorted by the compare function

List<E> sublist(int start, [int? end])